递归解法
List<Integer> path = null;
public List<Integer> preorderTraversal(TreeNode root) {
path = new ArrayList<Integer>();
dfs3(root);
return path;
}
/**
* 前序
* @param root
*/
public void dfs(TreeNode root) {
if (root == null) {
return;
}
path.add(root.val);
dfs(root.left);
dfs(root.right);
}
/**
* 中序
* @param root
*/
public void dfs2(TreeNode root) {
if (root == null) {
return;
}
dfs2(root.left);
path.add(root.val);
dfs2(root.right);
}
/**
* 后序
* @param root
*/
public void dfs3(TreeNode root) {
if (root == null) {
return;
}
dfs3(root.left);
dfs3(root.right);
path.add(root.val);
}
迭代解法
/**
* 迭代前序遍历
* @param root
* @return
*/
public List<Integer> preOrderIteration(TreeNode root) {
path = new ArrayList<Integer>();
if (root == null) {
return path;
}
Deque<TreeNode> stack = new LinkedList<TreeNode>();
while (!stack.isEmpty() || root != null) {
while (root != null) {
path.add(root.val);
stack.push(root);
root = root.left;
}
root = stack.pop();
root = root.right;
}
return path;
}
/**
* 迭代前序遍历
* @param root
* @return
*/
public List<Integer> preOrderIteration2(TreeNode root) {
path = new ArrayList<Integer>();
if (root == null) {
return path;
}
Deque<TreeNode> stack = new LinkedList<TreeNode>();
stack.push(root);
while (!stack.isEmpty()) {
TreeNode head = stack.pop();
path.add(head.val);
if (head.right != null) {
stack.push(head.right);
}
if (head.left != null) {
stack.push(head.left);
}
}
return path;
}
/**
* 迭代中序遍历
* @param head
*/
public List<Integer> inOrderIteration(TreeNode root) {
List<Integer> path = new ArrayList<Integer>();
if (root == null) {
return path;
}
Deque<TreeNode> stack = new LinkedList<TreeNode>();
TreeNode cur = root;
while (!stack.isEmpty() || cur != null) {
while (cur != null) {
stack.push(cur);
cur = cur.left;
}
TreeNode head = stack.pop();
path.add(head.val);
if (head.right != null) {
cur = head.right;
}
}
return path;
}
/**
* 迭代后续遍历
* @param args
*/
public List<Integer> postOrderIteration(TreeNode root) {
List<Integer> path = new ArrayList<Integer>();
if (root == null) {
return path;
}
Deque<TreeNode> stack1 = new LinkedList<TreeNode>();
Deque<TreeNode> stack2 = new LinkedList<TreeNode>();
stack1.push(root);
while (!stack1.isEmpty()) {
TreeNode head = stack1.pop();
stack2.push(head);
if (head.left != null) {
stack1.push(head.left);
}
if (head.right != null) {
stack1.push(head.right);
}
}
while (!stack2.isEmpty()) {
path.add(stack2.pop().val);
}
return path;
}
/**
* 迭代后续遍历
* @param args
*/
public List<Integer> postOrderIteration2(TreeNode root) {
List<Integer> path = new ArrayList<Integer>();
if (root == null) {
return path;
}
TreeNode cur = root;
Deque<TreeNode> stack = new LinkedList<TreeNode>();
stack.push(root);
while (!stack.isEmpty()) {
TreeNode peek = stack.peek();
if (peek.left != null && peek.left != cur && peek.right != cur) {
stack.push(peek.left);
} else if (peek.right != null && peek.right != cur) {
stack.push(peek.right);
} else {
path.add(stack.pop().val);
cur = peek;
}
}
return path;
}
Morris解法
/**
* Morris前序遍历
* @param root
* @return
*/
public List<Integer> preOrderMorris(TreeNode root) {
List<Integer> path = new ArrayList<Integer>();
if (root == null) {
return path;
}
TreeNode cur1 = root;
TreeNode cur2 = null;
while (cur1 != null) {
cur2 = cur1.left;
if (cur2 != null) {
while (cur2.right != null && cur2.right != cur1) {
cur2 = cur2.right;
}
if (cur2.right == null) {
cur2.right = cur1;
path.add(cur1.val);
cur1 = cur1.left;
continue;
} else {
cur2.right = null;
}
} else {
path.add(cur1.val);
}
cur1 = cur1.right;
}
return path;
}
/**
* Morris中序遍历
* @param root
* @return
*/
public List<Integer> inOrderMorris(TreeNode root) {
List<Integer> path = new ArrayList<Integer>();
if (root == null) {
return path;
}
TreeNode cur1 = root;
TreeNode cur2 = null;
while (cur1 != null) {
cur2 = cur1.left;
if (cur2 != null) {
while (cur2.right != null && cur2.right != cur1) {
cur2 = cur2.right;
}
if (cur2.right == null) {
cur2.right = cur1;
cur1 = cur1.left;
continue;
} else {
cur2.right = null;
}
}
path.add(cur1.val);
cur1 = cur1.right;
}
return path;
}
/**
* Morris后续遍历
* @param root
* @return
*/
public List<Integer> postOrderMorris(TreeNode root) {
List<Integer> path = new ArrayList<Integer>();
if (root == null) {
return path;
}
TreeNode cur1 = root;
TreeNode cur2 = null;
while (cur1 != null) {
cur2 = cur1.left;
if (cur2 != null) {
while (cur2.right != null && cur2.right != cur1) {
cur2 = cur2.right;
}
if (cur2.right == null) {
cur2.right = cur1;
cur1 = cur1.left;
continue;
} else {
cur2.right = null;
addPath(path, cur1.left);
}
}
cur1 = cur1.right;
}
addPath(path, root);
return path;
}
public void addPath(List<Integer> res, TreeNode node) {
int count = 0;
while (node != null) {
++count;
res.add(node.val);
node = node.right;
}
int left = res.size() - count, right = res.size() - 1;
while (left < right) {
int temp = res.get(left);
res.set(left, res.get(right));
res.set(right, temp);
left++;
right--;
}
}