算法day14-二叉树(4)

目录

  1. 找树左下角的值
  2. 路径总和
  3. 从中序与后序遍历序列构造二叉树

一、找树左下角的值

https://leetcode.cn/problems/find-bottom-left-tree-value/?envType=problem-list-v2&envId=8At1GmaZ

   方法一:迭代。

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

  方法二:递归。

 

class Solution {
    static int maxDepth;
    static int res;
    public int findBottomLeftValue(TreeNode root) {
        maxDepth = Integer.MIN_VALUE;
        res = 0;
        dfs(root,0);
        return res;
    }

    public void dfs(TreeNode root, int depth){
        //终止条件: 当遇到叶子节点时,记录第一次深度最深的叶子节点
        if(root.left == null && root.right == null){
            if(depth > maxDepth){
                maxDepth = depth;
                res = root.val;
            }
            return;
        }
        if(root.left != null){
            dfs(root.left, depth+1);
        }
        if(root.right != null){
            dfs(root.right, depth+1);
        }
    }
}

二、路径总和

https://leetcode.cn/problems/path-sum/?envType=problem-list-v2&envId=8At1GmaZ

 

class Solution {
    static int sum;
    public boolean hasPathSum(TreeNode root, int targetSum) {
        if(root == null){
            return false;
        }
        sum = 0;
        return dfs(root, sum, targetSum);
    }

    public boolean dfs(TreeNode root, int sum,int targetSum){
        sum += root.val;
        //终止条件
        if(root.left == null && root.right == null && sum == targetSum){
            return true;
        }
        if(root.left != null){
            if(dfs(root.left, sum, targetSum)){
                return true;
            }
        }
        if(root.right != null){
            if(dfs(root.right, sum, targetSum)){
                return true;
            }
        }
        return false;
    }
}

【扩展题:路经总和II】

https://leetcode.cn/problems/path-sum-ii/description/

class Solution {
    static List<List<Integer>> res;
    static List<Integer> path;
    public List<List<Integer>> pathSum(TreeNode root, int targetSum) {
        res = new ArrayList<>();
        path = new ArrayList<>();
        if(root == null){
            return res;
        }
        int sum = 0;
        path.add(root.val);
        dfs(root, sum, targetSum);
        return res;
    }

    public void dfs(TreeNode root, int sum, int targetSum){
        sum += root.val;
        if(root.left == null && root.right == null){
            if(sum == targetSum){
                res.add(new ArrayList<>(path));
            }
            return;
        }
        if(root.left != null){
            path.add(root.left.val);
            dfs(root.left, sum, targetSum);
            path.remove(path.size()-1);
        }
        if(root.right != null){
            path.add(root.right.val);
            dfs(root.right, sum, targetSum);
            path.remove(path.size()-1);
        }
    }
}

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

https://leetcode.cn/problems/construct-binary-tree-from-inorder-and-postorder-traversal/description/?envType=problem-list-v2&envId=8At1GmaZ

 

public class Solution {
    private HashMap<Integer, Integer> inorderMap; // 存储中序遍历的值和索引
    private int postIndex; // 指向后序遍历的当前根节点位置

    public TreeNode buildTree(int[] inorder, int[] postorder) {
        // 初始化中序遍历的哈希映射(值 -> 索引)
        inorderMap = new HashMap<>();
        for (int i = 0; i < inorder.length; i++) {
            inorderMap.put(inorder[i], i);
        }
        postIndex = postorder.length - 1; // 后序遍历的根节点在末尾
        return buildTreeHelper(postorder, 0, inorder.length - 1);
    }

    private TreeNode buildTreeHelper(int[] postorder, int inStart, int inEnd) {
        if (inStart > inEnd) {
            return null; // 递归终止条件:无节点可构造
        }

        // 当前根节点的值(后序遍历的最后一个元素)
        int rootVal = postorder[postIndex];
        TreeNode root = new TreeNode(rootVal);
        postIndex--; // 移动到下一个根节点(右子树优先)

        // 在中序遍历中找到根节点的位置
        int rootPos = inorderMap.get(rootVal);

        // 递归构建右子树(注意顺序:后序遍历是左右根,倒序时先右后左)
        root.right = buildTreeHelper(postorder, rootPos + 1, inEnd);
        // 递归构建左子树
        root.left = buildTreeHelper(postorder, inStart, rootPos - 1);

        return root;
    }
}

 

【扩展题:从前序和中序遍历构造二叉树】

https://leetcode.cn/problems/construct-binary-tree-from-preorder-and-inorder-traversal/description/

 

posted @ 2025-05-12 23:35  筱倩  阅读(9)  评论(0)    收藏  举报