513.找树左下角的值
class Solution {
int leftValue = 0;
int Mdeep = -1;
public void findLeft(TreeNode root,int deep){
if(root == null)return;
deep++;
if(deep > Mdeep){
leftValue = root.val;
Mdeep = deep;
}
if(root.left!= null){
findLeft(root.left,deep);
}
if(root.right != null){
findLeft(root.right,deep);
}
}
public int findBottomLeftValue(TreeNode root) {
if(root == null)return 0;
findLeft(root,0);
return leftValue;
}
}
113 路径总和
class Solution {
public void findPath(TreeNode root, int targetSum, List<Integer> path,List<List<Integer>> res){
if(root == null) return;
targetSum -= root.val;
path.add(root.val);
if(root.left == null && root.right == null && targetSum == 0){
res.add(new ArrayList<>(path));
}
if(root.left!=null){
findPath(root.left,targetSum,path,res);
path.remove(path.size()-1);
}
if(root.right!=null){
findPath(root.right,targetSum,path,res);
path.remove(path.size()-1);
}
}
public List<List<Integer>> pathSum(TreeNode root, int targetSum) {
List<List<Integer>> res = new ArrayList<>();
List<Integer> path = new ArrayList<>();
findPath(root,targetSum,path,res);
return res;
}
}