Day 16 | 104.MaximumDepth of BinaryTree| 111.MinimumDepth of BinaryTree| 222.CountComplete TreeNodes

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

Directory

  • LeetCode 104. Maximum Depth of Binary Tree
  • LeetCode 111. Minimum Depth of Binary Tree
  • LeetCode 222. Count Complete Tree Nodes


LeetCode 104. Maximum Depth of Binary Tree

Question Link

Solution:
1、Recursion(get depth)

class Solution {
    int max = 0;
    public int maxDepth(TreeNode root) {
        getDepth(root, 0);
        return max;
    }

    void getDepth(TreeNode node, int depth){
        if(node == null) return;
        depth++;
        max = max < depth ? depth : max;
        getDepth(node.left, depth);
        getDepth(node.right, depth);
    }
}

2、Recursion(get height)

class Solution {
    public int maxDepth(TreeNode root) {
        if(root == null) return 0;
        int leftDepth = maxDepth(root.left);
        int rightDepth = maxDepth(root.right);
        return 1 + Math.max(leftDepth, rightDepth);
    }

}
  • The height of the root node is the max depth of a binary tree. So in this question, we get the max depth by using postorder traversal to calculate the height of the root node

3、Iterative Method(lever order)

class Solution {
    public int maxDepth(TreeNode root) {
        if(root == null)
            return 0;
        Deque<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        int depth = 0;
        while(!queue.isEmpty()){
            int size = queue.size();
            depth++;
            while(size > 0){
                TreeNode node = queue.poll();
                if(node.left != null) queue.offer(node.left);
                if(node.right != null) queue.offer(node.right);
                size--;
            }
        }
        return depth;
    }
}

LeetCode 111. Minimum Depth of Binary Tree

Question Link
Solution:
1、Recursion(get height)

class Solution {
    public int minDepth(TreeNode root) {
        return getDepth(root);
    }

    int getDepth(TreeNode node){
        if(node == null) return 0;
        if(node.left == null && node.right != null)
            return getDepth(node.right) + 1;
        if(node.left != null && node.right == null)
            return getDepth(node.left) + 1;
        if(node.left == null && node.right == null)
            return 1;

        return Math.min(getDepth(node.left), getDepth(node.right)) + 1;
    }
}
  • If the left subtree is null and the right subtree is not null. The minimum depth is the depth of the right subtree + 1.
  • If the right subtree is null and the left subtree is not null. The minimum depth is the depth of the left subtree + 1.
  • If both left and right subtrees are not null, return the minimum depth of the left and right subtrees + 1.

2、Iterative(lever order)

class Solution {
    public int minDepth(TreeNode root) {
        if(root == null)
            return 0;
        Deque<TreeNode> deque = new LinkedList<>();
        deque.offer(root);
        int depth = 0;
        while(!deque.isEmpty()){
            int size = deque.size();
            depth++;
            while(size > 0){
                TreeNode node = deque.poll();
                if(node.left == null && node.right == null)
                    return depth;
                if(node.left != null) deque.offer(node.left);
                if(node.right != null) deque.offer(node.right);
                size--;
            }
        }
        return depth;
    }
}
  • When traversal, if both the left and right child nodes are null, return depth directly. Because the traversal order is from top to bottom, this is the minimum depth.

LeetCode 222. Count Complete Tree Nodes

Question Link
Solution:
1、Recursion

class Solution {
    public int countNodes(TreeNode root) {
        if(root == null)
            return 0;
        return countNodes(root.left) + countNodes(root.right) + 1;
    }
}

2、Iterative(lever order)

class Solution {
    public int countNodes(TreeNode root) {
        if(root == null)
            return 0;
        Deque<TreeNode> deque = new LinkedList<>();
        deque.add(root);
        int count = 0;
        while(!deque.isEmpty()){
            int size = deque.size();           
            while(size > 0){
                TreeNode node = deque.poll();
                count++;
                if(node.left != null) deque.offer(node.left);
                if(node.right != null) deque.offer(node.right);
                size--;
            }
        }
        return count;
    }
}

3、Complete Tree Solution

class Solution {
    public int countNodes(TreeNode root) {
        if(root == null)
            return 0;
        TreeNode left = root.left;
        TreeNode right = root.right;
        int leftDepth = 0, rightDepth = 0;
        // Calculate the depth of left subtree
        while(left != null) {
            left = left.left;
            leftDepth++;
        }
        // Calculate the depth of right subtree
        while(right != null){
            right = right.right;
            rightDepth++;
        }
        if(leftDepth == rightDepth)
            return (2 << leftDepth)-1;

        return countNodes(root.left) + countNodes(root.right) + 1;
    }
}
  • The node number of the complete tree is 2^depth -1.
  • 2<<1 is equivalent to 2^2

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