- 题目说明
- 知识点及方法综述
- 总结
一.题目说明
1.1leetcode pathsum 给定一棵二叉树和一个值,判断树当中是否存在从根节点到叶子节点的路径上面的值等于给定的值。
1.2leetcode pahtsumII 给定二叉树和一个值,返回所有等于该值得路径。
二.知识点及方法综述
2.1知识点:①二叉树的深度优先遍历②递归过程中间值保存
2.2两题思路综述:①先序遍历二叉树的结点,同时②将路径保存到相应的栈结构当中,如果满足③其左右子树均为空,并且④其和等于给定的值,作相应的⑤题目要求操作(1.1为返回true;1.2为保存相应的路径)
pathsum代码如下:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool hasPathSum(TreeNode* root, int sum)
{
if(!root)
return false;
int currentSum=0;
vector<int> path;
bool flag=false;
findPathSum(root,sum,path,currentSum,flag);
if(flag)
return true;
else
return false;
}
void findPathSum(TreeNode*root,int sum,vector<int>&path,int¤tSum,bool &flag)
{
if(flag)
return ;
path.push_back(root->val);
currentSum+=root->val;
if(!root->left&&!root->right&¤tSum==sum)
{
flag=true;
}
if(root->left)
findPathSum(root->left,sum,path,currentSum,flag);
if(root->right)
findPathSum(root->right,sum,path,currentSum,flag);
currentSum-=root->val;
path.pop_back();
}
};
pathsumI Python代码:运用递归方法做sum修正
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
def hasPathSum(self, root, sum):
"""
:type root: TreeNode
:type sum: int
:rtype: bool
"""
if not root:
return False
if root.val==sum and not root.left and not root.right:
return True
if self.hasPathSum(root.left,sum-root.val) or self.hasPathSum(root.right,sum-root.val):
return True
return False
pathsumII代码如下:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<vector<int>> pathSum(TreeNode* root, int sum)
{
vector<vector<int>>pathAll;
if(!root)
return pathAll;
vector<int>path;
int currentSum=0;
findPathSum(root,sum,path,currentSum,pathAll);
return pathAll;
}
void findPathSum(TreeNode*root,int sum,vector<int>&path,int¤tSum,vector<vector<int>>&pathAll)
{
path.push_back(root->val);
currentSum+=root->val;
if(!root->left&&!root->right&¤tSum==sum)
{
//保存相应路径
pathAll.push_back(path);
}
if(root->left)
findPathSum(root->left,sum,path,currentSum,pathAll);
if(root->right)
findPathSum(root->right,sum,path,currentSum,pathAll);
currentSum-=root->val;
path.pop_back();
}
};
pathsumII Python代码:用dfs实现并记录相关路径
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
def pathSum(self, root, sum):
"""
:type root: TreeNode
:type sum: int
:rtype: List[List[int]]
"""
if not root:
return []
stack,res=[(root,[root.val],root.val)],[]
while stack:
temp,tempList,sumCount=stack.pop()
if not temp.left and not temp.right and sumCount==sum:
res+=[tempList]
if temp.right:
stack.append((temp.right,tempList+[temp.right.val],sumCount+temp.right.val))
if temp.left:
stack.append((temp.left,tempList+[temp.left.val],sumCount+temp.left.val))
return res
三.总结
3.1①树的知识点对于学习图章节的前提,树即图的特殊形式②深刻理解深度优先遍历(DFS)3.2让我们一同努力,明天会更好!