Day18(513, 112, 113, 106, 105)

513. Find Bottom Left Tree Value

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

Example 1:

Input: root = [2,1,3]
Output: 1

Example 2:

Input: root = [1,2,3,4,null,5,6,null,null,7]
Output: 7

class Solution {  
    public int findBottomLeftValue(TreeNode root) {  
        Deque<TreeNode> queue = new LinkedList<>();  
        queue.addLast(root);  
        int result = 0;  
        while (!queue.isEmpty()) {  
            int size = queue.size();  
            for (int i = 0; i < size; i++) {  
                TreeNode node = queue.pollFirst();  
                if (i == 0) result = node.val;  
                if (node.left != null) queue.addLast(node.left);  
                if (node.right != null) queue.addLast(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.

Example 1:

Input: root = [5,4,8,11,null,13,4,7,2,null,null,null,1], targetSum = 22
Output: true
Explanation: The root-to-leaf path with the target sum is shown.

Example 2:

Input: root = [1,2,3], targetSum = 5
Output: false
Explanation: There two root-to-leaf paths in the tree:
(1 --> 2): The sum is 3.
(1 --> 3): The sum is 4.
There is no root-to-leaf path with sum = 5.

Example 3:

Input: root = [], targetSum = 0
Output: false
Explanation: Since the tree is empty, there are no root-to-leaf paths.

class Solution {  
    public boolean hasPathSum(TreeNode root, int targetSum) {  
        if (root == null) return false;  
        return getSum(root, 0, targetSum);  
    }  
  
    public static boolean getSum(TreeNode node, int sum, int targetSum) {  
        sum += node.val;  
        if (isLeaf(node)) return sum == targetSum;  
        else {  
            boolean boolLeft = false, boolRight = false;  
            if (node.left != null) {  
                boolLeft = getSum(node.left, sum, targetSum);  
            }  
            if (node.right != null) {  
                boolRight = getSum(node.right, sum, targetSum);  
            }  
            return boolLeft || boolRight;  
        }  
    }  
  
    public static boolean isLeaf(TreeNode node) {return node.left == null && node.right == null;}  
}

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.

Example 1:

Input: root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22
Output: [[5,4,11,2],[5,8,4,5]]
Explanation: There are two paths whose sum equals targetSum:
5 + 4 + 11 + 2 = 22
5 + 8 + 4 + 5 = 22

Example 2:

Input: root = [1,2,3], targetSum = 5
Output: []

Example 3:

Input: root = [1,2], targetSum = 0
Output: []

class Solution {  
    List<List<Integer>> list = new ArrayList<>();  
  
    public List<List<Integer>> pathSum(TreeNode root, int targetSum) {  
        if (root == null) return list;  
        getList(root, 0, targetSum, new ArrayList<>());  
        return this.list;  
    }  
  
    public void getList(TreeNode node, int sum, int targetSum, List<Integer> list) {  
        sum += node.val;  
        list.add(list.size(), node.val);  
        if (isLeaf(node)) {  
            if (sum == targetSum) this.list.add(list);  
        }  
        if (node.left != null) getList(node.left, sum, targetSum, new ArrayList<>(list));  
        if (node.right != null) getList(node.right, sum, targetSum, new ArrayList<>(list));  
    }  
  
    public static boolean isLeaf(TreeNode node) {return node.left == null && node.right == null;}  
}

心得:我写的方法难度不大,但是消耗内存太多,代码随想录使用回溯空间就小得多

class Solution {  
    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<>();  
        preorderDFS(root, targetsum, res, path);  
        return res;  
    }  
  
    public void preorderDFS(TreeNode root, int targetSum, List<List<Integer>> res, List<Integer> path) {  
        path.add(root.val);  
        // 遇到了叶子节点  
        if (root.left == null && root.right == null) {  
            // 找到了和为 targetsum 的路径  
            if (targetSum - root.val == 0) {  
                res.add(new ArrayList<>(path));  
            }  
            return; // 如果和不为 targetsum,返回  
        }  
  
        if (root.left != null) {  
            preorderDFS(root.left, targetSum - root.val, res, path);  
            path.remove(path.size() - 1); // 回溯  
        }  
        if (root.right != null) {  
            preorderDFS(root.right, targetSum - root.val, res, path);  
            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.

Example 1:

Input: inorder = [9,3,15,20,7], postorder = [9,15,7,20,3]
Output: [3,9,20,null,null,15,7]

Example 2:

Input: inorder = [-1], postorder = [-1]
Output: [-1]

class Solution {  
    public TreeNode buildTree(int[] inorder, int[] postorder) {  
        Map<Integer, Integer> indexMap = new HashMap<>();  
        for (int i = 0; i < inorder.length; i++) {  
            indexMap.put(inorder[i], i);  
        }  
        return buildTreeHelper(inorder, 0, inorder.length - 1, postorder, 0, postorder.length - 1, indexMap);  
    }  
  
    private TreeNode buildTreeHelper(int[] inorder, int inStart, int inEnd, int[] postorder, int postStart, int postEnd, Map<Integer, Integer> indexMap) {  
        if (inStart > inEnd || postStart > postEnd) {  
            return null;  
        }  
        int rootVal = postorder[postEnd];  
        TreeNode root = new TreeNode(rootVal);  
        int rootIndex = indexMap.get(rootVal);  
        int leftSize = rootIndex - inStart;  
        int rightSize = inEnd - rootIndex;  
        root.left = buildTreeHelper(inorder, inStart, rootIndex - 1, postorder, postStart, postStart + leftSize - 1, indexMap);  
        root.right = buildTreeHelper(inorder, rootIndex + 1, inEnd, postorder, postEnd - rightSize, postEnd - 1, indexMap);  
        return root;  
    }  
}

心得:自己想想不出来

105. Construct Binary Tree from Preorder and Inorder Traversal

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

Example 1:

Input: preorder = [3,9,20,15,7], inorder = [9,3,15,20,7]
Output: [3,9,20,null,null,15,7]

Example 2:

Input: preorder = [-1], inorder = [-1]
Output: [-1]

class Solution {  
    public TreeNode buildTree(int[] preorder, int[] inorder) {  
        Map<Integer, Integer> indexMap = new HashMap<>();  
        for (int i = 0; i < inorder.length; i++) {  
            indexMap.put(inorder[i], i);  
        }  
        return buildTreeHelper(preorder, 0, preorder.length - 1, inorder, 0, inorder.length - 1, indexMap);  
    }  
  
    private TreeNode buildTreeHelper(int[] preorder, int preStart, int preEnd, int[] inorder, int inStart, int inEnd, Map<Integer, Integer> indexMap) {  
        if (preStart > preEnd || inStart > inEnd) {  
            return null;  
        }  
        int rootVal = preorder[preStart];  
        TreeNode root = new TreeNode(rootVal);  
        int rootIndex = indexMap.get(rootVal);  
        int leftSize = rootIndex - inStart;  
        int rightSize = inEnd - rootIndex;  
        root.left = buildTreeHelper(preorder, preStart + 1, preStart + leftSize, inorder, inStart, rootIndex - 1, indexMap);  
        root.right = buildTreeHelper(preorder, preEnd - rightSize + 1, preEnd, inorder, rootIndex + 1, inEnd, indexMap);  
        return root;  
    }  
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值