1. Topics
- Maximum depth of a binary tree: Given a binary tree, find its maximum depth (the number of path nodes farthest from the root node)
- Minimum depth of a binary tree: Given a binary tree, find its minimum depth (the number of path nodes nearest to the root node)
- Generate effective parentheses: Given the number n, write a function that combines all closed parentheses to output. n represents the logarithm of the generated parentheses.
2. Algorithmic problem solving
2.1 Maximum depth of a binary tree: Given a binary tree, find its maximum depth (the number of path nodes farthest from the root node)
Solution 1: Depth First DFS, Recursion
The maximum depth of the left and right subtrees is calculated separately, and then 1 is the maximum depth of the current binary tree.
Time complexity O(n), space complexity O(1)
public int maxDepth(TreeNode root){ if (root == null){ return 0; }else{ int left = maxDepth(root.left); int right = maxDepth(root.right); int max = Math.max(left, right) + 1; return max; } }
2.2 Minimum Depth of a Binary Tree: Given a Binary Tree, find its Minimum Depth (Number of Path Nodes nearest to the root node)
Solution 1: Depth First DFS, Recursion
The minimum depth of the left and right subtrees is calculated separately, and then 1 is the minimum depth of the current binary tree.
Time complexity O(n), space complexity O(1)
public int minDepth (TreeNode root){ if (root == null) return 0; if (root.left== null && root.right == null) return 1; int minDepth = Integer.MIN_VALUE; if (root.left != null) minDepth = Math.min(minDepth(root.left), minDepth); if (root.right != null) minDepth = Math.min(minDepth(root.right), minDepth); return minDepth + 1; }
Solution 2: breadth first search, iteration
With queue assistance, the TreeNode is removed sequentially. As long as the first node is a leaf node, the depth of the current node can be returned.
public int minDepth(TreeNode root){ LinkedList<Pair<TreeNode, Integer>> pairs = new LinkedList<>(); if (root == null) return 0; //Place the root node in the queue pairs.add(new Pair<TreeNode, Integer>(root, 1)); int minDepth = 0; while (!pairs.isEmpty()){ // FIFO Pair<TreeNode, Integer> pair = pairs.poll(); TreeNode node = pair.first; minDepth = pair.second; if (node.left == null && node.right == null) break; if (node.left != null) pairs.add(new Pair<TreeNode, Integer>(node.left, minDepth +1)); if (node.right != null) pairs.add(new Pair<TreeNode, Integer>(node.right, minDepth + 1)); } return minDepth; }
2.3 Generate effective parentheses: Given the number n, write a function that combines all closed parentheses to output. n represents the logarithm of parentheses.
Solution 1: Backtracking
To determine whether parentheses are valid, continue adding valid parentheses until left and right parentheses match, and when the string length is 2*n, add them to the collection. The problem is more abstract when recursive residual logic is executed and exhausted.
Time Complexity O(2n) Space Complexity O(n)
public static List generateParenthesis(int n){
List combinations = new ArrayList<>();
addGenerateOneByOne(combinations, "", 0, 0 ,n); return combinations; } private static void addGenerateOneByOne(List<String> combinations, String s, int open, int close, int max) { if (s.length() == max *2) combinations.add(s); if (open < max) addGenerateOneByOne(combinations, s.concat("("), open +1, close, max); if (close < open) addGenerateOneByOne(combinations, s.concat(")"), open, close + 1, max); }