【力扣刷题】Day18——二叉树专题


上一篇文章:【力扣刷题】Day17——二叉树专题_塔塔开!!!的博客-CSDN博客

13. 找树左下角的值

题目链接:513. 找树左下角的值 - 力扣(LeetCode)

被题目搞晕了:左下角的值并不是值左叶子节点!!!!

思路:

深度优先搜索:按照先左后右的顺序遍历子树,最先搜索到的最深的结点即所求的结点(更新深度判断即可)

class Solution {
    
    
    int res = 0;
    int max_h = 0;
    public int findBottomLeftValue(TreeNode root) {
    
    
        dfs(root, 1);
        return res;
    }
    public void dfs(TreeNode root, int depth){
    
    
        if(root == null){
    
    
            return ;
        }
        
        // 遍历的第一个最深的叶子节点肯定就是最底层的最左节点(前序遍历嘛)
        if(root.left == null && root.right == null){
    
    
            // 证明是当前深度的最左边叶子节点,因为先递归左子树
            if(depth > max_h){
    
    
                res = root.val;
                max_h = depth;
            }

            if(root.left != null)
                dfs(root.left, depth + 1);
            if(root.right != null)
                dfs(root.right, depth + 1);    
        }
    }
}

14. 路径总和I

题目链接:112. 路径总和 - 力扣(LeetCode)

思路:dfs从根节点遍历到叶子节点,判断这条路径是否符合要求即可。

Code

class Solution {
    
    
    public boolean hasPathSum(TreeNode root, int targetSum) {
    
    
        return dfs(root, targetSum);
    }
    public boolean dfs(TreeNode root, int targetSum){
    
    
        if(root == null){
    
    
            return false;
        }
        // 到达叶子节点,判断这条路径是否符合要求
        if(root.left == null && root.right == null){
    
    
            return targetSum - root.val == 0;
        }
        return dfs(root.left, targetSum - root.val) || dfs(root.right, targetSum - root.val);
    }
}

15. 路径总和II

题目链接:113. 路径总和 II - 力扣(LeetCode)

DFS:爆搜版,每一条完整的路径都要新建一个tmp_list来记录,最终(叶子节点且满足条件)在存入res————不用回溯

缺点:时间费在大量创建tmp_list上!

Code

class Solution {
    
    
    List<List<Integer>> result = new ArrayList<>();
    public List<List<Integer>> pathSum(TreeNode root, int sum) {
    
    
        dfs(root, sum, new ArrayList<>());
        return result;
    }
    public void dfs(TreeNode root, int sum, List<Integer> list) {
    
    
        //如果节点为空直接返回
        if (root == null){
    
    
            return;
        }
        sum -= root.val;
        //因为list是引用传递,为了防止递归的时候分支污染,我们要在每个路径
        //每一条完整的路径对应一个tmp
        //中都要新建一个(subList)tmp(在上一次tmp的基础上)
        List<Integer> tmp = new ArrayList(list);
        tmp.add(root.val);
        
        if (root.left == null && root.right == null && sum == 0) {
    
    
            result.add(tmp);
            return;
        }

        dfs(root.left, sum, tmp);
        dfs(root.right, sum, tmp);
    }
}

回溯版:这里之所以要回溯,那是因为我们始终在用一个list集合在记录res,当到达满足条件的叶子节点时,将本次路径重新new 出来加入res而已,为此就要回溯(弹出本次选中的末尾元素),为下一个路径做好准备!

class Solution {
    
    
    List<List<Integer>> res = new ArrayList<>();
    List<Integer> list = new ArrayList<>();

    public List<List<Integer>> pathSum(TreeNode root, int targetSum) {
    
    
        dfs(root, targetSum);
        return res;
    }

    public void dfs(TreeNode root, int sum){
    
    
        if(root == null){
    
    
            return ;
        }
        sum -= root.val;// 选值

        // 到达叶子节点
        if(root.left == null && root.right == null && sum == 0){
    
    
            list.add(root.val);
            res.add(new ArrayList(list));
            list.remove(list.size() - 1);// 要是在递归出口中加入叶子节点,最后一定要弹出(回溯!!!)
            return ;
        }

        // 左 右
        list.add(root.val);
        dfs(root.left, sum);
        dfs(root.right, sum);
        list.remove(list.size() - 1);// 回溯

    }
}

16. 路径总和III

题目链接:437. 路径总和 III - 力扣(LeetCode)

双重DFS:我们遍历每一个节点,从这个节点开始计算它的子树满足要求的路径。

我们访问每一个节点 node,检测以node 为起始节点(头节点)且向下延深的路径有多少种(第二次dfs判断左右子树是否右满足的情况)。我们递归遍历每一个节点的所有可能的路径,然后将这些路径数目加起来即为返回结果。

Code

class Solution {
    
    
    int res = 0;
    public int pathSum(TreeNode root, int targetSum) {
    
    
        if(root == null){
    
    
            return 0;
        }
        long longTargetSum = targetSum;
        dfs(root, longTargetSum);// 每一个根节点都要dfs判断
        // 为下一次dfs做好准备
        pathSum(root.left, targetSum);
        pathSum(root.right, targetSum);
        return res;
    }
    public void dfs(TreeNode root, long sum){
    
    
        if(root == null){
    
    
            return ;
        }
        sum -= root.val;
        if(sum == 0){
    
    
            rse ++;
            // 这里不能return !! 下面可能还要答案集
        }
        dfs(root.left, sum);
        dfs(root.right, sum);
    }
}

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

题目链接:106. 从中序与后序遍历序列构造二叉树 - 力扣(LeetCode)

具体思路回顾之前的博客:二叉树的遍历_塔塔开!!!的博客-CSDN博客_二叉树遍历

Code

class Solution {
    
    
    // 将中序的值对应位置记下,方便后面找到中序跟所在位置
    Map<Integer, Integer> pos = new HashMap<>();
    int hou[];
    public TreeNode buildTree(int[] inorder, int[] postorder) {
    
    
        int n = inorder.length;
        hou = postorder;
        for(int i = 0; i < n; i ++) pos.put(inorder[i], i);

        TreeNode root = build(0, n - 1, 0, n - 1);
        return root;
    }
    /**
        由中和后序构造二叉树
        build:返回二叉树的根节点
     */
    public TreeNode build(int il, int ir, int pl, int pr){
    
    
        if(il > ir || pl > pr) return null;

        int root = hou[pr];
        int k = pos.get(root);
        int x = k - 1 - il + pl;

        // 递归创建左右子树
        TreeNode node = new TreeNode(root);
        node.left = build(il, k - 1, pl, x);
        node.right = build(k + 1, ir, x + 1, pr - 1);

        return node;

    }
}

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

题目链接:105. 从前序与中序遍历序列构造二叉树 - 力扣(LeetCode)

思路:看上一题即可

Code

class Solution {
    
    
    Map<Integer, Integer> mp = new HashMap<Integer, Integer>();
    int[] qian;
    public TreeNode buildTree(int[] pre, int[] in) {
    
    
        int n = pre.length;
        qian = pre;
        for(int i = 0; i < n; i ++) mp.put(in[i], i);
        return build(0, n - 1, 0, n - 1);
    }
    public TreeNode build(int pl, int pr, int il, int ir){
    
    
        if(pl > pr || il > ir) return null;

        int root = qian[pl];
        int k = mp.get(root);
        TreeNode node = new TreeNode(root);
        int x = k - 1 - il + pl + 1;

        node.left = build(pl + 1, x, il, k - 1);
        node.right = build(x + 1, pr, k + 1, ir);
        return node;
    }
}

猜你喜欢

转载自blog.csdn.net/qq_54773252/article/details/127205583
今日推荐