代码随想录算法训练营Day 18| 找树左下角的值、路径总和、从中序与后序遍历序列构造二叉树

513.找树左下角的值

代码随想录

思路:

层序遍历,遍历每一层时记录第一个poll的值,代表每一层最左侧元素,遍历完时,res就变成了树左下角的值

代码:

class Solution {
    public int findBottomLeftValue(TreeNode root) {
        Queue<TreeNode> que = new LinkedList<>();
        que.add(root);
        int res = root.val;
        while(!que.isEmpty()){
            int size = que.size();
            for(int i = 0; i < size; i++){
                TreeNode node = que.poll();
                if(i == 0){
                    res = node.val;
                }
                if(node.left != null){
                    que.add(node.left);
                }
                if(node.right != null){
                    que.add(node.right);
                }
            }
        }
        return res;
    }
}

需要注意的点:

112. 路径总和

代码随想录

思路:

与遍历二叉树所有路径思路类似,在碰到满足条件的路径时返回true

代码:

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

    private boolean process(TreeNode root, int targetSum){
        //终止条件
        if(root.left == null && root.right == null && targetSum == 0) return true;
        if(root.left == null && root.right == null) return false;

        if(root.left != null){
            if(process(root.left, targetSum - root.left.val)) return true;
        }
        if(root.right != null){
            if(process(root.right, targetSum - root.right.val)) return true;
        }
        return false;
    }
}

需要注意的点:

1、递归中隐藏着回溯,因为传入的值为targetSum - num,没有改变targetSum的值。

2、终止条件需要理解

113. 路径总和ii

思路:

代码:

class Solution {
    public List<List<Integer>> pathSum(TreeNode root, int targetSum) {
        List<List<Integer>> res = new ArrayList<>();
        if(root == null) return res;
        List<Integer> paths = new LinkedList<>();
        process(root, targetSum, res, paths);
        return res;
    }

    private void process(TreeNode root, int targetSum, List<List<Integer>> res, List<Integer> paths){
        paths.add(root.val);
        if(root.left == null && root.right == null){
            if(targetSum - root.val == 0){
                res.add(new ArrayList<>(paths));
            }
            return;
        }
        if(root.left != null){
            process(root.left, targetSum - root.val, res, paths);
            paths.remove(paths.size() - 1);
        }
        if(root.right != null){
            process(root.right, targetSum - root.val, res, paths);
            paths.remove(paths.size() - 1);
        }
    }
}

需要注意的点:

1、注意和上一个题在处理节点值上的区别,第一个题传入参数代表的意思是不包含root的和,本题是包含root.

2、注意paths加入res的写法,因为paths是会变动的,不能直接加入,需要复制。

106.从中序与后序遍历序列构造二叉树

代码随想录

思路:

代码:

class Solution {
    Map<Integer, Integer> map = new HashMap<>();
    public TreeNode buildTree(int[] inorder, int[] postorder) {
        
        for(int i = 0; i < inorder.length; i++){
            map.put(inorder[i], i);
        }
        //区间采用左闭右开
        return findNode(inorder, 0, inorder.length, postorder, 0, postorder.length);
    }

    private TreeNode findNode(int[] inorder, int inbegin, int inend, int[] postorder, int postbegin, int postend){
        if(inbegin >= inend || postbegin >= postend){//超出界限说明没有节点
            return null;
        }
        TreeNode root = new TreeNode(postorder[postend - 1]);
        int index = map.get(postorder[postend - 1]);//中间节点在中序的位置
        int len = index - inbegin;//搜索左节点的长度,在两个顺序的遍历中共享
        root.left = findNode(inorder, inbegin, index, postorder, postbegin, postbegin + len);
        root.right = findNode(inorder, index + 1, inend, postorder, postbegin + len, postend - 1);
        return root;
    }
}

需要注意的点:

105.从前序与中序遍历序列构造二叉树

思路:

代码:

需要注意的点:

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值