代码随想录算法训练营第十八天| LeetCode513 找树左下角的值 、LeetCode112 路径总和 、LeetCode106 从中序与后序遍历序列构造二叉树

LeetCode513 找树左下角的值

题目链接:https://programmercarl.com/0513.%E6%89%BE%E6%A0%91%E5%B7%A6%E4%B8%8B%E8%A7%92%E7%9A%84%E5%80%BC.html代码:

public class code513 {
    int Deep = -1;
    int res = 0;

    // 迭代法(层序遍历)
    public int findBottomLeftValue(TreeNode root) {

        Queue<TreeNode> queue = new LinkedList();
        int res = 0;
        queue.offer(root);

        while (!queue.isEmpty()) {

            int size = queue.size();
            for (int i = 0; i < size; i++) {
                // 如果i=0,说明当前node为这一层的最左侧节点
                TreeNode node = queue.poll();
                if (i == 0) res = node.val;

                if (node.left != null) {
                    queue.offer(node.left);
                }
                if (node.right != null) {
                    queue.offer(node.right);
                }
            }
        }
        return res;
    }

    // 递归法
    public int findBottomLeftValue1(TreeNode root) {
        res = root.val;
        findLeftValue(root, 0);
        return res;
    }


    private void findLeftValue(TreeNode root, int deep) {
        if (root == null) return;
        if (root.left == null && root.right == null) {
            if (deep > Deep) {
                res = root.val;
                Deep = deep;
            }
        }
        if (root.left != null) findLeftValue(root.left, deep + 1);  // 左
        if (root.right != null) findLeftValue(root.right, deep + 1);  // 右
    }
}

两种方法迭代和递归,迭代很好理解,层序遍历,遍历到最后一层找到第一个就可以,递归法注意回溯过程

LeetCode112 路径总和

题目链接:https://programmercarl.com/0112.%E8%B7%AF%E5%BE%84%E6%80%BB%E5%92%8C.html代码:

public class code112 {
    public boolean hasPathSum(TreeNode root, int targetSum) {

        if (root == null) return false;

        // 遇到叶子节点,判断是否等于target,因为target是一层层减下去的
        if (root.left == null && root.right == null) {
            return root.val == targetSum;
        }
        if (root.left != null) {
            boolean left = hasPathSum(root.left, targetSum - root.val); // 注意理解回溯过程
            if (left) {
                return true;
            }
        }
        if (root.right != null) {
            boolean right = hasPathSum(root.right, targetSum - root.val);
            if (right) {
                return true;
            }
        }
        return false;
    }
}

113

代码:

public class code113 {
    public List<List<Integer>> pathSum(TreeNode root, int targetSum) {

        List<List<Integer>> res = new ArrayList<>();
        if (root == null) return res;

        List<Integer> path = new LinkedList<>();
        findPath(root, targetSum, res, path);
        return res;
    }

    private void findPath(TreeNode root, int targetSum, List<List<Integer>> res, List<Integer> path) {
        path.add(root.val);

        if (root.right == null && root.left == null) {
            if (targetSum - root.val == 0) {
                res.add(new ArrayList<>(path));
            }
            return;
        }
        if (root.left != null) {
            findPath(root.left, targetSum - root.val, res, path);
            path.remove(path.size() - 1);
        }
        if (root.right != null) {
            findPath(root.right, targetSum - root.val, res, path);
            path.remove(path.size() - 1);
        }
    }
}

注意比较两道题不同,一个需要返回值,一个不需要

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

题目链接:https://programmercarl.com/0106.%E4%BB%8E%E4%B8%AD%E5%BA%8F%E4%B8%8E%E5%90%8E%E5%BA%8F%E9%81%8D%E5%8E%86%E5%BA%8F%E5%88%97%E6%9E%84%E9%80%A0%E4%BA%8C%E5%8F%89%E6%A0%91.html代码:

public class code106 {

    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); // 把中序遍历中对应的数值以及位置保存到map中
        }
        TreeNode root = findNode(inorder, 0, inorder.length, postorder, 0, postorder.length); // 左闭右开
        return root;
    }

    private TreeNode findNode(int[] inorder, int inBegin, int inEnd, int[] postorder, int postBegin, int postEnd) {
        // 参数范围都是左闭右开
        if (inBegin >= inEnd || postBegin >= postEnd) {
            return null;
        }
        // 找到根节点在中序遍历的位置
        int rootIndex = map.get(postorder[postEnd - 1]);
        TreeNode root = new TreeNode(inorder[rootIndex]);
        int len = rootIndex - inBegin; // 保存中序左子树的个数,用来分割后序序列

        root.left = findNode(inorder, inBegin, rootIndex, postorder, postBegin, postBegin + len);
        root.right = findNode(inorder, rootIndex + 1, inEnd, postorder, postBegin + len, postEnd - 1);

        return root;
    }
}

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值