2018-10-03 20:16:53
非遞歸遍歷二叉樹是使用堆棧來進行保存,個人推薦使用雙while結構,完全按照遍歷順序來進行堆棧的操作,當然在前序和后序的遍歷過程中還有其他的壓棧流程。
一、Binary Tree Preorder Traversal
問題描述:

問題求解:
先序遍歷就是在第一次訪問到節點的時候將其值進行打印,然后遞歸打印其左子樹,最后遞歸打印其右子樹。
解法一、雙while
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> res = new ArrayList<>();
Stack<TreeNode> stack = new Stack<>();
while (!stack.isEmpty() || root != null) {
while (root != null) {
res.add(root.val);
stack.push(root);
root = root.left;
}
root = stack.pop();
root = root.right;
}
return res;
}
解法二、
可以使用一個棧來模擬這種操作:
首先將root壓棧;
每次從堆棧中彈出棧頂元素,表示當前訪問的元素,對其進行打印;
依次判斷其右子樹,左子樹是否非空,並進行壓棧操作,至於為什么先壓棧右子樹,因為先壓棧的后彈出,左子樹需要先訪問,因此后壓棧;
重復直到棧為空。
/**
* 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) {
if (root == null) return new ArrayList<>();
List<Integer> res = new ArrayList<>();
Stack<TreeNode> stack = new Stack<>();
stack.push(root);
while (!stack.isEmpty()) {
TreeNode cur = stack.pop();
res.add(cur.val);
if (cur.right != null) stack.push(cur.right);
if (cur.left != null) stack.push(cur.left);
}
return res;
}
}
二、Binary Tree Inorder Traversal
問題描述:

問題求解:
雙while大法。
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> res = new ArrayList<>();
Stack<TreeNode> stack = new Stack<>();
while (!stack.isEmpty() || root != null) {
while (root != null) {
stack.push(root);
root = root.left;
}
root = stack.pop();
res.add(root.val);
root = root.right;
}
return res;
}
三、Binary Tree Postorder Traversal
問題描述:

問題求解:
后序遍歷的非遞歸版本是最有技巧性的,難度相對高一點,但其實是可以轉換成前序遍歷的。
后序遍歷的順序是左右中,因此只需要按中右左遍歷,再reverse一下即可。
雙while大法
public List<Integer> postorderTraversal(TreeNode root) {
List<Integer> res = new ArrayList<>();
Stack<TreeNode> stack = new Stack<>();
while (!stack.isEmpty() || root != null) {
while (root != null) {
res.add(0, root.val);
stack.push(root);
root = root.right;
}
root = stack.pop();
root = root.left;
}
return res;
}
