Path Sum and Delete Node in a Linked List
2017-04-08 15:51
274 查看
今天照例更新数据结构基础,希望各位各取所需,我本人是习惯通过题目通过code来加深对数据结构以及某些算法的理解。
现在首先我们还是来看看二叉树相关的题目:
Path sum,leetcode上给出的演示如下:
For example:
Given the below binary tree and sum = 22,
return true, as there exist a root-to-leaf path 5->4->11->2 which sum is 22.
题目判定,能否在某二叉树root中找到 各节点和为sum的路径。
首先老规矩分两种方法,DFS和BFS(注意因为要用到队列,故而BFS 是通过C++来实现的)。
首先我们看,通过BFS我们一层一层就遍历
算法分析:准备两个队列,一个为node,保存节点,一个为value,保存当前节点与之前节点的之和的值,下面看代码:
好了BSF方法已经完成,比较直接不需要太多思维,接下来看看DFS
DFS显得简洁很多,直接利用递归。
再看链表相关的:
Delete Node in a Linked List
感觉这个题目没什么好讲的直接看代码:
现在首先我们还是来看看二叉树相关的题目:
Path sum,leetcode上给出的演示如下:
For example:
Given the below binary tree and sum = 22,
5 / \ 4 8 / / \ 11 13 4 / \ \ 7 2 1
return true, as there exist a root-to-leaf path 5->4->11->2 which sum is 22.
题目判定,能否在某二叉树root中找到 各节点和为sum的路径。
首先老规矩分两种方法,DFS和BFS(注意因为要用到队列,故而BFS 是通过C++来实现的)。
首先我们看,通过BFS我们一层一层就遍历
算法分析:准备两个队列,一个为node,保存节点,一个为value,保存当前节点与之前节点的之和的值,下面看代码:
public class Solution { public boolean hasPathSum(TreeNode root, int sum) { if(root == NULL) return false; queue<TreeNode*> node; queue<int> value; node.push(root); value.push(root->val); // 开启遍历 while(node.size() > 0) { TreeNode* cur = node.front(); node.pop(); int sumvalue = value.front; if(cur->left == NULL && cur->right == NULL && sumvalue == sum){ return true; } if(cur->left !=NULL){ node.push(cur->left); value.push(sumvalue + cur->left->val); } if(cur->right !=NULL) { node.push(cur->right); vaule.push(sumvalue + cur->right->val); } } return false; }
好了BSF方法已经完成,比较直接不需要太多思维,接下来看看DFS
DFS显得简洁很多,直接利用递归。
public class Solution { public boolean hasPathSum(TreeNode root, int sum) { if(root == NULL) return false; if(root->left == NULL && root->right == NULL && root->val == sum) return true; // 开启递归 return hasPathSum(root->left, sum- root->val) ||hasPathSum(root->right, sum - root->val); }
再看链表相关的:
Delete Node in a Linked List
感觉这个题目没什么好讲的直接看代码:
void deleteNode(struct ListNode* node) { node->val = node->next->val; struct ListNode* tmp = node->next; node->next = tmp->next; free(tmp); }
相关文章推荐
- "same tree" and "Delete Node in a Linked List"
- LeetCode 237. Delete Node in a Linked List
- LeetCode - Delete Node in a Linked List
- LeetCode 237. Delete Node in a Linked List
- [Leetcode] 237. Delete Node in a Linked List 解题报告
- [Leetcode]237. Delete Node in a Linked List
- LintCode Delete Node in the Middle of Singly Linked List 在O(1)时间复杂度删除链表节点
- Leetcode 237 Delete Node in a Linked List
- LeetCode Delete Node in a Linked List
- Leetcode #237 Delete Node in a Linked List
- leetcode Delete Node in a Linked List--重要
- 372. 在O(1)时间复杂度删除链表节点 (delete-node-in-the-middle-of-singly-linked-list)(c++)----lintcode面试题之链表
- 237. Delete Node in a Linked List
- LeetCode Delete Node in a Linked List
- LeetCode--Delete Node in a Linked List(删除链表中的节点)Python
- Lintcode372 Delete Node in the Middle of Singly Linked List solution 题解
- LeetCode_delete-node-in-a-linked-list
- LeetCode Delete Node in a Linked List 链表
- Delete Node in a Linked List
- Delete Node in a Linked List