`

LeetCode - Binary Tree Traversal

 
阅读更多

Pre-order

preorder(node)
  if node == null then return
  visit(node)
  preorder(node.left) 
  preorder(node.right)
iterativePreorder(node)
  parentStack = empty stack
  while (not parentStack.isEmpty() or node ≠ null)
    if (node ≠ null) 
      visit(node)
      if(node.right ≠ null) parentStack.push(node.right) 
      node = node.left   
    else     
      node = parentStack.pop()

In-order

inorder(node)
  if node == null then return
  inorder(node.left)
  visit(node)
  inorder(node.right)
iterativeInorder(node)
  parentStack = empty stack
  while (not parentStack.isEmpty() or node ≠ null)
    if (node ≠ null)
      parentStack.push(node)
      node = node.left
    else
      node = parentStack.pop()
      visit(node)
      node = node.right

Post-order

postorder(node)
  if node == null then return
  postorder(node.left)
  postorder(node.right)
  visit(node)
iterativePostorder(node)
  parentStack = empty stack  
  lastnodevisited = null 
  while (not parentStack.isEmpty() or node ≠ null)
    if (node ≠ null)
      parentStack.push(node)
      node = node.left
    else
      peeknode = parentStack.peek()
      if (peeknode.right ≠ null and 
          lastnodevisited ≠ peeknode.right) 
        /* if right child exists AND traversing node 
         from left child, move right */
        node = peeknode.right
      else
        visit(peeknode)
        lastnodevisited = parentStack.pop() 

 

Pre-order:

public List<Integer> preorderTraversal(TreeNode node) {
    List<Integer> list = new LinkedList<Integer>();
    Stack<TreeNode> stack = new Stack<TreeNode>();
    while(!stack.isEmpty() || node != null) {
        if(node != null) {
            list.add(node.val);
            if(node.right != null) stack.push(node.right);
            node = node.left;
        } else {
            node = stack.pop();
        }
    }
    return list;
}

 

In-order:

private List<Integer> inorderIterative(TreeNode node) {
    List<Integer> list = new ArrayList<Integer>();
    Stack<TreeNode> stack = new Stack<TreeNode>();
    while(!stack.isEmpty() || node != null) {
        if(node != null) {
            stack.push(node);
            node = node.left;
        } else {
            TreeNode p = stack.pop();
            list.add(p.val);
            node = p.right;
        }
    }
    return list;
}

 

Posr-order:

private List<Integer> postorderIterative(TreeNode node) {
    List<Integer> list = new ArrayList<Integer>();
    Stack<TreeNode> stack = new Stack<TreeNode>();
    TreeNode last = null;
    while(!stack.empty() || node != null) {
        if(node != null) {
            stack.push(node);
            node = node.left;
        } else {
            TreeNode p = stack.peek();
            if(p.right != null && p.right != last) {
                node = p.right;
            } else {
                list.add(p.val);
                last = stack.pop();
            }
        }
    }
    return list;
}

 

Reference:

http://en.wikipedia.org/wiki/Tree_traversal#Implementations

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics