代码随想录-Day18

1.LeetCode 513. 找树左下角的值

题目链接:力扣刷题

参考资料代码随想录

视频讲解:B站视频讲解

给定一个二叉树的 根节点 root,请找出该二叉树的 最底层 最左边 节点的值。

假设二叉树中至少有一个节点。

示例 1:

输入: root = [2,1,3]
输出: 1

迭代法可能要比递归要简单很多。

本题使用层序遍历再合适不过了,比递归要好理解得多!

只需要记录最后一行第一个节点的数值就可以了。

java代码如下:

每一层遍历记录第一个值即可,后续一直覆盖,直至最后,返回result

class Solution {
    public int findBottomLeftValue(TreeNode root) {
        Queue<TreeNode> queue = new LinkedList<>();
        if(root == null){
            return 0;
        }
        queue.offer(root);
        int result =0;
        while (!queue.isEmpty()){
            int len = queue.size();
            for(int i =0;i < len;i++){
                TreeNode temp = queue.poll();
                if(temp.left != null){
                    queue.offer(temp.left);
                }
                if(temp.right != null){
                    queue.offer(temp.right);
                }
                if(i == 0){
                    result = temp.val;
                }
            }
        }
        return result;
    }
}

 递归法:

1.确定递归函数的参数和返回值

参数必须有要遍历的树的根节点,还有就是一个int型的变量用来记录最长深度。 这里就不需要返回值了,所以递归函数的返回类型为void。

本题还需要类里的两个全局变量,maxLen用来记录最大深度,result记录最大深度最左节点的数值。

2.确定终止条件

当遇到叶子节点的时候,就需要统计一下最大的深度了,所以需要遇到叶子节点来更新最大深度。

 3.确定单层递归的逻辑

在找最大深度的时候,递归的过程中依然要使用回溯

 具体java代码如下:

class Solution {
    int maxdeth = Integer.MIN_VALUE;
    int value =0;
    public int findBottomLeftValue(TreeNode root) {
        value = root.val;
        reverse(root,0);
        return value;

    }
    public  void reverse(TreeNode root,int deth){
        // 当root == null 的时候,返回0
        if(root == null){
            return;
        }
        // 终止条件:当节点左右孩子分别为空的时候,判断当前深度是否为最大深度,如果是最大深度,那就覆盖深度值和最左值
        if(root.left == null && root.right == null){
            if(deth > maxdeth){
                maxdeth = deth;
                value = root.val;
            }
        }
        // 采用前序,中节点不用处理,deth记录了统计几次,最后减一是开始进行回溯
        if(root.left != null){
            deth ++;
            reverse(root.left,deth);
            deth --;
        }
        if(root.right != null){
            deth ++;
            reverse(root.right,deth);
            deth --;
        }

    }
}

2.力扣刷题 112.路径总和和113题

题目链接:力扣刷题

力扣刷题代码随想录

视频讲解:代码随想录

给你二叉树的根节点 root 和一个表示目标和的整数 targetSum 。判断该树中是否存在 根节点到叶子节点 的路径,这条路径上所有节点值相加等于目标和 targetSum 。如果存在,返回 true ;否则,返回 false 。

叶子节点 是指没有子节点的节点。

示例 1:

输入:root = [5,4,8,11,null,13,4,7,2,null,null,null,1], targetSum = 22
输出:true
解释:等于目标和的根节点到叶节点路径如上图所示。

递归思路:

1.确认返回值和传参:

返回值是boolean类型,还要传入一个tragetsum,到时候用tragetsum-节点值;

2.确定终止条件:

当tragetsum==0的时候,并且左右节点都为空的时候返回。

3.确定单边递归条件:

当root.left或者right不为空的时候,传入root.left和right,并且tragetsum-root.left.val;

这一块代码是用来回溯的,当当前节点已经走到最后了,任然没有,那我们就得走另外一条路了

具体java代码如下:

class Solution {
    public boolean hasPathSum(TreeNode root, int targetSum) {
        if(root == null){
            return  false;
        };
        return reverser(root,targetSum-root.val);

    }
    
    public boolean reverser(TreeNode root,int targetSum){
        if(root.left == null && root.right == null && targetSum == 0){
            return true;
        }
        if(root.left == null && root.right == null && targetSum != 0){
            return false;
        }
        if(root.left != null){
            targetSum -= root.left.val;
            if(reverser(root.left,targetSum)){
                return  true;
            }
            targetSum += root.left.val;
        }
        if(root.right != null){
            targetSum -= root.right.val;
            if(reverser(root.right,targetSum)){
                return  true;
            }
            targetSum += root.right.val;
        }
        return false;
    }
}

 迭代法:

java代码如下:

class Solution {
    public boolean hasPathSum(TreeNode root, int targetSum) {
        Stack<TreeNode> treeNodeStack = new Stack<>();
        Stack<Integer> sumStack = new Stack<>();

        if(root == null)
            return false;
        treeNodeStack.add(root);
        sumStack.add(root.val);

        while(!treeNodeStack.isEmpty()){
            TreeNode curr = treeNodeStack.peek();
            int tempsum = sumStack.pop();
            if(curr != null){
                treeNodeStack.pop();
                treeNodeStack.add(curr);
                treeNodeStack.add(null);
                sumStack.add(tempsum);
                if(curr.right != null){
                    treeNodeStack.add(curr.right);
                    sumStack.add(tempsum + curr.right.val);
                }
                if(curr.left != null){
                    treeNodeStack.add(curr.left);
                    sumStack.add(tempsum + curr.left.val);
                }
            }else{
                treeNodeStack.pop();
                TreeNode temp = treeNodeStack.pop();
                if(temp.left == null && temp.right == null && tempsum == targetSum)
                    return true;
            }
        }
        return false;
    }
}

113题代码:

class Solution {
    List<List<Integer>> result;
    LinkedList<Integer> path;
    public List<List<Integer>> pathSum (TreeNode root,int targetSum) {
        result = new LinkedList<>();
        path = new LinkedList<>();
        travesal(root, targetSum);
        return result;
    }
    private void travesal(TreeNode root,  int count) {
        if (root == null) return;
        path.offer(root.val);
        count -= root.val;
        if (root.left == null && root.right == null && count == 0) {
            result.add(new LinkedList<>(path));
        }
        travesal(root.left, count);
        travesal(root.right, count);
        path.removeLast(); // 回溯
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值