This method will cost O(n) time with no extra space. The space is assigned by system(which could be O(1) or O(h), h is the height of the tree).
1 2 3 4 5 6 7 8 9 10 11 12 13 14
public ArrayList<Integer> preorderTraversal(TreeNode root){ ArrayList<Integer> result = new ArrayList<Integer>(); helper(root, result); return result; }
Use a stack to store the nodes. Pay attention to the order of storing. We store the right subtree node before the left one, so that we can get the preorder nodes when pop from the stack. This method will cost O(n) time with O(h) extra space for the stack.
*/ publicclassSolution{ public ArrayList<Integer> preorderTraversal(TreeNode root){ ArrayList<Integer> result = new ArrayList<Integer>(); if(root == null) { return result; } Stack<TreeNode> stack = new Stack<TreeNode>(); stack.push(root); while(!stack.isEmpty()) { TreeNode cur = stack.pop(); result.add(cur.val); if(cur.right != null) { stack.push(cur.right); } if(cur.left != null) { stack.push(cur.left); } } return result; } }
1.3 Divide & Conquer
The different between this method and the recursion one is the return type: this method returns an arraylist, but the recursion method doesn’t return anything. This method will cost O(n) time with O(n) space.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
publicclassSolution{ public ArrayList<Integer> preorderTraversal(TreeNode root){ ArrayList<Integer> result = new ArrayList<Integer>(); if(root == null) { return result; }
Divide & Conquer method is useful in sorting algorithm.
1.3.1 Quick Sort
This is the most useful sorting algorithm in industry. Its average time complexity is O(nlogn), but in worst case, it could be O(n^2) when the pivot we found is not ideal. This algorithm is in-place(no extra space needs) but not stable(stable: if after sorting [2,2’,1,1’], we get[1,1’,2,2’], we call it stable, otherwise it is not stable). Related Problems: Partition Array Sort Color II Median
1.3.2 Merge Sort
This algorithm costs O(nlogn) time and O(n) space. Although it is a stable algorithm, the space costs and the “move”(move the sorted result from temp space to the original position) step make it not that useful like quick sort. Related Problems: Merge sorted array II Merge k sorted lists
publicclassSolution{ public ArrayList<ArrayList<Integer>> levelOrder(TreeNode root) { ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>(); if(root == null) { return result; }
LinkedList<TreeNode> queue = new LinkedList<TreeNode>(); ArrayList<Integer> level = new ArrayList<Integer>(); queue.add(root); int lastNum = 1; int curNum = 0;
Find the minimum node in the right subtree (or the maximum node in the left subtree)
Replace the node with the minimum node in the right subtree (or the maximum node in the left subtree) Replace: If node.right == null: replace the node with its left child(parent.child = node.left) Else if node has right child: replace the node with the smallest one that in its right subtree. To do that, we keep two nodes(temp and father), tracking to find the smallest node and replace it.
Comments