Tree
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}
DFS
Preorder:
Iterative using stack:
public class Solution {
public List preorderTraversal(TreeNode root) {
Stack stack = new Stack();
List preorder = new ArrayList();
if (root == null) {
return preorder;
}
stack.push(root);
while (!stack.empty()) {
TreeNode node = stack.pop();
preorder.add(node.val);
if (node.right != null) {
stack.push(node.right);
}
if (node.left != null) {
stack.push(node.left);
}
}
return preorder;
}
}
Recursive:
public class Solution {
public ArrayList preorderTraversal(TreeNode root) {
ArrayList result = new ArrayList();
traverse(root, result);
return result;
}
// 把root为跟的preorder加入result里面
private void traverse(TreeNode root, ArrayList result) {
if (root == null) {
return;
}
result.add(root.val);
traverse(root.left, result);
traverse(root.right, result);
}
}
Inorder:
Recursive:
class Solution {
public List < Integer > inorderTraversal(TreeNode root) {
List < Integer > res = new ArrayList < > ();
helper(root, res);
return res;
}
public void helper(TreeNode root, List < Integer > res) {
if (root != null) {
if (root.left != null) {
helper(root.left, res);
}
res.add(root.val);
if (root.right != null) {
helper(root.right, res);
}
}
}
}
Iterative:
public class Solution {
public ArrayList inorderTraversal(TreeNode root) {
Stack stack = new Stack();
ArrayList result = new ArrayList();
TreeNode curt = root;
while (curt != null || !stack.empty()) {
while (curt != null) {
stack.add(curt);
curt = curt.left;
}
curt = stack.pop();
result.add(curt.val);
curt = curt.right;
}
return result;
}
}
Postorder:
Recursive:
public ArrayList postorderTraversal(TreeNode root) {
ArrayList result = new ArrayList();
if (root == null) {
return result;
}
result.addAll(postorderTraversal(root.left));
result.addAll(postorderTraversal(root.right));
result.add(root.val);
return result;
}
Iterative:
public ArrayList postorderTraversal(TreeNode root) {
ArrayList result = new ArrayList();
Stack stack = new Stack();
TreeNode prev = null; // previously traversed node
TreeNode curr = root;
if (root == null) {
return result;
}
stack.push(root);
while (!stack.empty()) {
curr = stack.peek();
if (prev == null || prev.left == curr || prev.right == curr) { // traverse down the tree
if (curr.left != null) {
stack.push(curr.left);
} else if (curr.right != null) {
stack.push(curr.right);
}
} else if (curr.left == prev) { // traverse up the tree from the left
if (curr.right != null) {
stack.push(curr.right);
}
} else { // traverse up the tree from the right
result.add(curr.val);
stack.pop();
}
prev = curr;
}
return result;
}
Recursive: Validate binary search tree
public class Solution {
public boolean isValidBST(TreeNode root) {
return divConq(root, Long.MIN_VALUE, Long.MAX_VALUE);
}
private boolean divConq(TreeNode root, long min, long max){
if (root == null){
return true;
}
if (root.val <= min || root.val >= max){
return false;
}
return divConq(root.left, min, Math.min(max, root.val)) &&
divConq(root.right, Math.max(min, root.val), max);
}
}
Level Order BFS
class Solution {
public List> levelOrder(TreeNode root) {
Queue queue = new LinkedList();
List> wrapList = new LinkedList>();
if(root == null) return wrapList;
queue.offer(root);
while(!queue.isEmpty()) {
int levelNum = queue.size();
List sublist = new LinkedList();
for(int i=0; i