二叉树的前中后序遍历基于DFS深度优先遍历,递归写法很简单,迭代写法比较难。
递归法:
- 前序遍历
public void preDfs(TreeNode root) {
if(root != null) {
visit(root);
preDfs(root.left);
preDfs(root.right);
}
}
- 中序遍历
public void inDfs(TreeNode root) {
if(root != null) {
inDfs(root.left);
visit(root);
inDfs(root.right);
}
}
- 后序遍历
public void postDfs(TreeNode root) {
if(root != null) {
postDfs(root.left);
postDfs(root.right);
visit(root);
}
}
迭代法:
基于dfs就要用到栈数据结构。
- 前序遍历
遍历顺序为根左右,刚好可以边弹出节点,边压入右节点和左节点,因为栈的特性所以要注意两者的顺序。
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> res = new ArrayList<>();
if(root == null) return res;
Stack<TreeNode> stack = new Stack<>();
stack.push(root);
while(!stack.isEmpty()) {
TreeNode temp = stack.pop();
res.add(temp.val);
if(temp.right != null) stack.push(temp.right);
if(temp.left != null) stack.push(temp.left);
}
return res;
}
}
// 方法二 和下面中序遍历的思路差不多。只不过是压入栈之前就遍历节点,而中序遍历弹出之后再遍历。
class Solution {
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> result = new ArrayList<>();
if (root == null) return result;
Stack<TreeNode> stack = new Stack<>();
TreeNode cur = root;
while (cur != null || !stack.isEmpty()) {
while (cur != null) {
result.add(cur.val);
stack.push(cur);
cur = cur.left;
}
cur = stack.pop();
cur = cur.right;
}
return result;
}
}
- 中序遍历
遍历顺序为左根右,因为根在中间,所以不能先弹出根了。跟前序遍历不大一样,压入根节点后一直压入根节点的左子节点,之后弹出并访问最左边的节点,然后访问右节点进入下一个大循环,这里用curNode记录当前处理的节点,到这里一个循环。
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> res = new ArrayList<>();
Stack<TreeNode> stack = new Stack<>();
if(root == null) return res;
TreeNode curNode = root;
while(curNode != null || !stack.isEmpty()) {
while(curNode != null) {
stack.push(curNode);
curNode = curNode.left;
}
curNode = stack.pop();
res.add(curNode.val);
curNode = curNode.right;
}
return res;
}
}
- 后序遍历
遍历顺序为左右根,这里用了比较讨巧的方式,LinkedList允许在列表最前面插入值,那么就可以从根节点弹出并访问值,只不过插入的顺序总是在最前面。插入根的值后应该插入右子树的值了,所以右子树应该比左子树后入栈。
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public List<Integer> postorderTraversal(TreeNode root) {
LinkedList<Integer> res = new LinkedList<>();
if(root == null) return res;
Stack<TreeNode> stack = new Stack<>();
stack.push(root);
while(!stack.isEmpty()) {
TreeNode temp = stack.pop();
res.addFirst(temp.val);
if(temp.left != null) stack.push(temp.left);
if(temp.right != null) stack.push(temp.right);
}
return res;
}
}
class Solution {
public List<Integer> postorderTraversal(TreeNode root) {
List<Integer> result = new ArrayList<>();
if (root == null) return result;
Stack<TreeNode> stack = new Stack<>();
TreeNode cur = root;
TreeNode pre = null;
while (cur != null || !stack.isEmpty()) {
while (cur != null) {
stack.push(cur);
cur = cur.left;
}
cur = stack.peek();
if (cur.right == null || pre == cur.right) {
result.add(cur.val);
stack.pop();
pre = cur;
cur = null;
} else {
cur = cur.right;
pre = null;
}
}
return result;
}
}