BST 前序,中序,后序遍历,Iterative Method

来源:互联网 发布:淘宝开店要押金吗 编辑:程序博客网 时间:2024/06/05 12:42


1_Binary Tree Postorder Traversal
思路:
We use a prev variable to keep track of the previously-traversed node. Let’s assume curr is the current node that’s on top of the stack. When prev is curr‘s parent, we are traversing down the tree. In this case, we try to traverse to curr‘s left child if available (ie, push left child to the stack). If it is not available, we look at curr‘s right child. If both left and right child do not exist (ie, curr is a leaf node), we print curr‘s value and pop it off the stack.


If prev is curr‘s left child, we are traversing up the tree from the left. We look at curr‘s right child. If it is available, then traverse down the right child (ie, push right child to the stack), otherwise print curr‘s value and pop it off the stack.


If prev is curr‘s right child, we are traversing up the tree from the right. In this case, we print curr‘s value and pop it off the stack.


Prev是之前访问过的结点,但不一定是curr的父节点
栈顶元素是记录当前访问的结点
只有将值输出后才能在stack中pop

public ArrayList<Integer> postorderTraversal(TreeNode root) {ArrayList<Integer> result = new ArrayList<Integer>();if (root == null)return result;Stack<TreeNode> stack = new Stack<TreeNode>();stack.push(root);TreeNode prev = null;TreeNode curr;while (!stack.isEmpty()) {curr = stack.peek();// we are traversing down the treeif (prev == null || prev.left == curr || prev.right == curr) {if (curr.left != null)stack.push(curr.left);else if (curr.right != null)stack.push(curr.right);else {result.add(curr.val);stack.pop();}// we are traversing up the tree from the left} else if (curr.left == prev) {if (curr.right != null)stack.push(curr.right);else {result.add(curr.val);stack.pop();}// we are traversing up the tree from the right} else if (curr.right == prev) {result.add(curr.val);stack.pop();}// record previously traversed nodeprev = curr;}return result;}




关于stack的一些function:
Pop, push, peek






2_Binary Tree Preorder Traversal
Preorder binary tree traversal is a classic interview problem about trees. The key to solve this problem is to understand the following:


What is preorder? (parent node is processed before its children)
Use Stack from Java Core library
It is not obvious what preorder for some strange cases. However, if you draw a stack and manually execute the program, how each element is pushed and popped is obvious.


The key to solve this problem is using a stack to store left and right children, and push right child first so that it is processed after the left child.


public ArrayList<Integer> preorderTraversal(TreeNode root) {ArrayList<Integer> resultList = new ArrayList<Integer>();if (root == null)return resultList;Stack<TreeNode> stack = new Stack<TreeNode>();stack.push(root);while (!stack.isEmpty()) {TreeNode temp = stack.pop();resultList.add(temp.val);if (temp.right != null)stack.push(temp.right);if (temp.left != null)stack.push(temp.left);}return resultList;}


3_Binary Tree Inorder Traversal
public ArrayList<Integer> inorderTraversal(TreeNode root) {ArrayList<Integer> result = new ArrayList<Integer>();Stack<TreeNode> s = new Stack<TreeNode>();boolean done = false;TreeNode current = root;while (!done) {if (current != null) {s.push(current);current = current.left;} else {if (s.isEmpty())done = true;else {TreeNode temp = s.pop();result.add(temp.val);current = temp.right;}}}return result;}


挺难想的

0 0
原创粉丝点击