Day 18 | 513. Find Bottom Left Tree Value | 112. Path Sum | 105&106. Construct Binary Tree

Day 1 | 704. Binary Search | 27. Remove Element | 35. Search Insert Position | 34. First and Last Position of Element in Sorted Array
Day 2 | 977. Squares of a Sorted Array | 209. Minimum Size Subarray Sum | 59. Spiral Matrix II
Day 3 | 203. Remove Linked List Elements | 707. Design Linked List | 206. Reverse Linked List
Day 4 | 24. Swap Nodes in Pairs| 19. Remove Nth Node From End of List| 160.Intersection of Two Lists
Day 6 | 242. Valid Anagram | 349. Intersection of Two Arrays | 202. Happy Numbe | 1. Two Sum
Day 7 | 454. 4Sum II | 383. Ransom Note | 15. 3Sum | 18. 4Sum
Day 8 | 344. Reverse String | 541. Reverse String II | 替换空格 | 151.Reverse Words in a String | 左旋转字符串
Day 9 | 28. Find the Index of the First Occurrence in a String | 459. Repeated Substring Pattern
Day 10 | 232. Implement Queue using Stacks | 225. Implement Stack using Queue
Day 11 | 20. Valid Parentheses | 1047. Remove All Adjacent Duplicates In String | 150. Evaluate RPN
Day 13 | 239. Sliding Window Maximum | 347. Top K Frequent Elements
Day 14 | 144.Binary Tree Preorder Traversal | 94.Binary Tree Inorder Traversal| 145.Binary Tree Postorder Traversal
Day 15 | 102. Binary Tree Level Order Traversal | 226. Invert Binary Tree | 101. Symmetric Tree
Day 16 | 104.MaximumDepth of BinaryTree| 111.MinimumDepth of BinaryTree| 222.CountComplete TreeNodes
Day 17 | 110. Balanced Binary Tree | 257. Binary Tree Paths | 404. Sum of Left Leaves

Directory

  • LeetCode 513. Find Bottom Left Tree Value
  • LeetCode 112. Path Sum
  • LeetCode 113. Path Sum II
  • LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal
  • LeetCode 106. Construct Binary Tree from Inorder and Postorder Traversal


LeetCode 513. Find Bottom Left Tree Value

Question Link
1、Recursion Traversal

class Solution {
    public int findBottomLeftValue(TreeNode root) {
        int result = 0;
        Deque<TreeNode> deque = new LinkedList<>();
        deque.offer(root);
        while(!deque.isEmpty()){
            int size = deque.size();
            for(int i = 0; i < size; i++) {
                TreeNode node = deque.poll();
                if(i == 0) result = node.val; // firs left node
                if(node.left != null) deque.offer(node.left);
                if(node.right != null) deque.offer(node.right);
            }           
        }
        return result;
    }
}
  • The first left node in the last layer is the bottom left node.

2、Level Order Traversal

class Solution {
    int result;
    int MAX_DEPTH = Integer.MIN_VALUE;

    public int findBottomLeftValue(TreeNode root) {
        findLeftValue(root, 0);
        return result;
    }

    void findLeftValue(TreeNode node, int depth){
        if(node.left == null && node.right == null){
            if(depth > MAX_DEPTH){
                MAX_DEPTH = depth;
                result = node.val;
            }
            return;
        }   
        if(node.left!=null){
            depth++;
            findLeftValue(node.left, depth);
            depth--;
        }
        if(node.right!=null){
            depth++;
            findLeftValue(node.right, depth);
            depth--;
        }
    }
}

LeetCode 112. Path Sum

Question Link

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

        if(root.right != null){
            if(hasPathSum(root.right, targetSum - root.val))
                return true;
        }
        return false;
    }
}

LeetCode 113. Path Sum II

Question Link

class Solution {
    List<List<Integer>> result = new ArrayList<>();
    public List<List<Integer>> pathSum(TreeNode root, int targetSum) {
        if(root == null) return result;
        List<Integer> itemList = new ArrayList<>();
        getPathSum(itemList, root, targetSum);
        return result;
    }
    
    void getPathSum(List<Integer> itemList, TreeNode node, int targetSum){
        itemList.add(node.val);
        if(node.left == null && node.right == null){
            if(targetSum - node.val == 0){
                result.add(new ArrayList<>(itemList)); // must new a ArrayList
            }
        }
        if(node.left != null){
            getPathSum(itemList, node.left, targetSum - node.val);
            itemList.remove(itemList.size()-1);
        }
        if(node.right != null){
            getPathSum(itemList, node.right, targetSum - node.val);
            itemList.remove(itemList.size()-1);
        }
    }
}
  • We add an object reference when using list.add(). So if we new an object outside the loop and assign the value in the loop. The reference is unchanged. The list contains the last assigned value of the object.

LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal

Question Link

class Solution {
    public TreeNode buildTree(int[] preorder, int[] inorder) {
        // Step 1: Termaination condition
        if(preorder.length == 0)
            return null;
        
        // Step 2: The first value of the preorder array is the node element 
        TreeNode root = new TreeNode(preorder[0]);
        
        if(preorder.length == 1)
            return root;
        
        // Step 3: Find the split point
        int index = 0;
        for(int val : inorder){
            if(root.val == val)
                break;
            index++;
        }
        
        // Step 4: Split the inorder array
        int[] inorder_left = new int[index];
        int[] inorder_right = new int[preorder.length - index - 1];
        System.arraycopy(inorder, 0, inorder_left,0, index);
        System.arraycopy(inorder, index+1, inorder_right, 0, preorder.length - index - 1);

        // Step 5:Split the preorder array
        int[] preorder_left = new int[index];
        int[] preorder_right = new int[preorder.length - index -1];
        System.arraycopy(preorder, 1, preorder_left, 0, index);
        System.arraycopy(preorder, index+1, preorder_right, 0, preorder.length - index - 1);

        // Step 6: Recursion
        root.left = buildTree(preorder_left, inorder_left);
        root.right = buildTree(preorder_right, inorder_right);
        return root;
    }
}

LeetCode 106. Construct Binary Tree from Inorder and Postorder Traversal

Question Link

class Solution {
    public TreeNode buildTree(int[] inorder, int[] postorder) {
        // Step 1: Termination condition
        if(postorder.length == 0)
            return null;
        // Step 2: The last value of the postorder array is the node element.
        int rootValue = postorder[postorder.length - 1];
        TreeNode root = new TreeNode(rootValue);

        if(postorder.length == 1)
            return root;
        
        // Step 3: Find the split point
        int index = 0;
        for(; index < inorder.length; index++){
            if(inorder[index] == rootValue)
                break;
        }
        // Step 4: Split the inorder array
        int[] inorder_left = new int[index];
        int[] inorder_right = new int[inorder.length - index - 1];
        System.arraycopy(inorder, 0, inorder_left, 0, index);
        System.arraycopy(inorder, index+1, inorder_right, 0, inorder.length - index - 1);

        // Step 5: Split the postorder array
        int[] postorder_left = new int[index];
        int[] postorder_right = new int[postorder.length - index - 1];
        System.arraycopy(postorder, 0, postorder_left, 0, index);
        System.arraycopy(postorder, index, postorder_right, 0, postorder.length - index - 1);
        
        // Step 5:Recursion
        root.left = buildTree(inorder_left, postorder_left);
        root.right = buildTree(inorder_right, postorder_right);
        return root;
    }
}

你可能感兴趣的:(LeetCode,leetcode)