第六章 二叉树 part05

62 阅读1分钟

513. Find Bottom Left Tree Value

Given the root of a binary tree, return the leftmost value in the last row of the tree.

题目解析:

  • 层次遍历迭代记录每一层第一个节点即可

代码:

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

112. Path Sum

Given the root of a binary tree and an integer targetSum, return true if the tree has a root-to-leaf path such that adding up all the values along the path equals targetSum.

leaf is a node with no children.

题目解析:

  • 递归回溯求解

代码:

class Solution {
    public boolean hasPathSum(TreeNode root, int targetSum) {
        if (root == null) return false;
        if (root.left == null && root.right == null && root.val == targetSum) {
            return true;
        }
        boolean isLeft = false, isRight = false;
        if (root.left != null) {
            isLeft = hasPathSum(root.left, targetSum - root.val);
        }
        if (root.right != null) {
            isRight = hasPathSum(root.right, targetSum - root.val);
        }
        return isLeft || isRight;
    }

}

113. Path Sum II

Given the root of a binary tree and an integer targetSum, return all root-to-leaf paths where the sum of the node values in the path equals targetSum . Each path should be returned as a list of the node values, not node references.

root-to-leaf path is a path starting from the root and ending at any leaf node. A leaf is a node with no children.

题目解析:

  • 递归回溯

代码:

class Solution {
    public List<List<Integer>> pathSum(TreeNode root, int targetSum) {
        List<List<Integer>> result = new ArrayList<>();
        dfs(root, targetSum, new ArrayList<>(), result);
        return result;
    }

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

106. Construct Binary Tree from Inorder and Postorder Traversal

Given two integer arrays inorder and postorder where inorder is the inorder traversal of a binary tree and postorder is the postorder traversal of the same tree, construct and return the binary tree.

题目解析:

  • 后序遍历的最后一个元素为root元素,再去inorder中查找该root元素,那么再inorder中root元素的左边为左子树,右边为右子树,按此方法递归构建整棵树
  • 需要判断区间的大小是否小于1

代码:

class Solution {
    public TreeNode buildTree(int[] inorder, int[] postorder) {
        return build(inorder, postorder, 0, inorder.length - 1, 0, postorder.length - 1);
    }

    public TreeNode build(int[] inorder, int[] postorder, int inLeft, int inRight, int postLeft, int postRight) {
        if (inRight < inLeft || postRight < postLeft) {
            return null;
        }
        int rootVal = postorder[postRight];
        TreeNode root = new TreeNode(rootVal);
        int leftLen = 0, rightLen = 0;
        for (int i = inLeft; i<= inRight; i++) {
            if (inorder[i] == rootVal) {
                leftLen = i - inLeft;
                rightLen = inRight - i;
                break;
            }
        }
        root.left = build(inorder, postorder, inLeft, inLeft + leftLen - 1, postLeft, postLeft + leftLen - 1);
        root.right = build(inorder, postorder, inLeft + leftLen + 1, inRight, postLeft + leftLen, postRight - 1);
        return root;
    }
}

总结

  1. 用DFS递归求路径或者路径和时,通常需要使用回溯方法。
  2. 中序+前序或者中序+后序可以确定一棵树