Process analysis

    

  1. Stack = 5,  
  2. Push 3, Stack = 5, 3.    Pre = 5
  3. Current = 3, Pre = 5, Push 2 to the stack, stack = 5, 3, 2, Pre = 3
  4. Current = 2, Pre = 3, pop 2, PRINT 2, Stack = 5, 3.  Pre = 2
  5. Current = 3, push 4, Stack = 5, 3 , 4.  Pre = 3
  6. Current = 4,  4 is leaf node, pop 4.  PRINT 4, Pre = 4, Stack = 5, 3
  7. Current = 3, Pre = 4, 4 is right child of 3, it means it goes up from right side, all visited.  Pop one more element from stack, PRINT 3.  Pre = 3.  Stack = 5
  8. Current = 5, Pre = 3, 5 has right child, push 7, Stack = 5, 7, Pre = 5
  9. Current 7, which is right child of 5, meaning it's first time to visit 7, 7 has left child, push 6 to the stack.  Stack = 5, 7, 6    Pre = 7
  10. Current = 6, 6 is 7's left child, but 6 is leaf node, pop 6.  Print 6, Pre = 6. Stack = 5, 7
  11. Current = 7, 7 has right child?  yes, 8. Push 8 to the stack, Stack = 5,7,8.  Pre = 7
  12. Current = 8, 8 has no left child, has right child 10, push 10 to the stack.  Stack = 5, 7, 8, 10.  Pre = 8
  13. Current = 10, 10 is leaf node, pop 10, Print 10, Stack = 5,7,8, Pre = 10
  14. Current = 8, pop 8, Print 8, Stack = 5, 7, Pre = 8
  15. Current = 7, pop 7, Print 7, Stack = 5, Pre = 7
  16. Current = 5, pop 5, Print 5,  stack = empty
 public void PostOrderNonRecursion1StackV1()
{
Stack<TreeNode<T>> stack = new Stack<TreeNode<T>>(); if (root == null)
{
return;
} TreeNode<T> current = this.root;
TreeNode<T> pre = null; stack.Push(this.root); while (stack.Count != )
{
current = stack.Peek(); // top down
if (pre == null || pre.leftChild == current || pre.rightChild == current)
{
if (current.leftChild != null)
{
stack.Push(current.leftChild);
}
else if (current.rightChild != null)
{
stack.Push(current.rightChild);
}
else
{
// leaf node, have to pop
Console.WriteLine("visit node {0}", stack.Pop().data);
}
}
// move up case from left node
else if (current.leftChild == pre)
{
if (current.rightChild != null)
{
stack.Push(current.rightChild);
}
else
{
Console.WriteLine("visit node {0}", stack.Pop().data);
}
}
// move up from right , which means all done, need pop up next one to figure out next action
else if (current.rightChild == pre)
{
Console.WriteLine("visit node {0}", stack.Pop().data);
} // remember the previous node, not parent node
pre = current;
}
}

The solution above uses one stack.  If you use two stacks, there are the following two ways.

Solution 1 - do pre-order non-recursive firstly, but the sequence is root, right, left, not root, left and right.

Then store the result in stack 2, print stack 2, you will get the final result.  Put code as below.

 public void PostOrderNonRecursion2StacksV1()
{
Stack<TreeNode<T>> stack1 = new Stack<TreeNode<T>>();
Stack<TreeNode<T>> stack2 = new Stack<TreeNode<T>>(); TreeNode<T> current = root; if (current == null)
{
return;
} while (current != null)
{
stack2.Push(current);
Console.WriteLine(current.data);
stack1.Push(current);
current = current.rightChild;
} while (stack1.Count != )
{
current = stack1.Pop(); if (current.leftChild != null)
{
current = current.leftChild;
while (current != null)
{
stack2.Push(current);
stack1.Push(current);
current = current.rightChild;
}
}
} while (stack2.Count != )
{
Console.WriteLine("value = {0}", stack2.Pop().data);
}
}

Solution 2:

这个方法对于初学者很难想到,根节点先入栈,然后根节点出栈,存入栈2.  根节点的左孩子入栈,根节点的右孩子入栈。

然后右孩子出栈,入栈2. 右孩子的左右节点入栈1。这样栈2的元素实际从底往上是主根,右孩子,递归右左。  反过来就是先左后右, 再根节点。  下面是代码

  public void PostOrderNonRecursion2StackV2()
{
Stack<TreeNode<T>> stack1 = new Stack<TreeNode<T>>();
Stack<TreeNode<T>> stack2 = new Stack<TreeNode<T>>(); TreeNode<T> current = root;
if (current == null)
{
return;
} stack1.Push(current); while (stack1.Count != )
{
current = stack1.Pop();
if (current.leftChild != null)
{
stack1.Push(current.leftChild);
} if (current.rightChild != null)
{
stack1.Push(current.rightChild);
} stack2.Push(current);
} while (stack2.Count != )
{
Console.WriteLine("value = {0}", stack2.Pop().data);
}
}

Post Order traverse binary tree using non-recursive way的更多相关文章

  1. Binary Tree Level Order Traversal,Binary Tree Level Order Traversal II

    Binary Tree Level Order Traversal Total Accepted: 79463 Total Submissions: 259292 Difficulty: Easy G ...

  2. 37. Binary Tree Zigzag Level Order Traversal && Binary Tree Inorder Traversal

    Binary Tree Zigzag Level Order Traversal Given a binary tree, return the zigzag level order traversa ...

  3. CSharp Algorithm - How to traverse binary tree by breadth (Part II)

    /* Author: Jiangong SUN */ Here I will introduce the breadth first traversal of binary tree. The pri ...

  4. 35. Binary Tree Level Order Traversal && Binary Tree Level Order Traversal II

    Binary Tree Level Order Traversal OJ: https://oj.leetcode.com/problems/binary-tree-level-order-trave ...

  5. LeetCode: Binary Tree Level Order Traversal && Binary Tree Zigzag Level Order Traversal

    Title: Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to ...

  6. LeetCode之“树”:Binary Tree Level Order Traversal && Binary Tree Level Order Traversal II

    Binary Tree Level Order Traversal 题目链接 题目要求: Given a binary tree, return the level order traversal o ...

  7. LeetCode解题报告—— Unique Binary Search Trees & Binary Tree Level Order Traversal & Binary Tree Zigzag Level Order Traversal

    1. Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees) that ...

  8. Morris InOrder Traverse Binary Tree 无需使用递归和栈

    今天在切leetcode的时候看到一个Morris算法,用来中序遍历二叉树,非递归,O(1)空间.觉得很强大.记录一下. 基本思想是利用了Threaded Binary Tree. 步骤如下: cur ...

  9. programming review (c++): (2)binary tree, BFS, DFS, recursive, non-recursive

    1.二叉树定义 // Definition for a binary tree node. struct TreeNode { int val; TreeNode *left; TreeNode *r ...

随机推荐

  1. jQuery 查找属性

    jQuery 查找属性 示例: <div xsk='123' > <div xsk='456' > // 具有xsk属性的所有标签 $('[xsk]') // xsk属性等于1 ...

  2. opencv学习之路(19)、直方图

    一.概述 二.一维灰度直方图 #include "opencv2/opencv.hpp" #include<iostream> using namespace cv; ...

  3. (转载)Unity UGUI鼠标点击UI不受影响方法IsPointerOverGameObject

    这几天在做捕鱼达人游戏时发现,当鼠标点击UI时,炮台的子弹也会发射子弹,这样会影响用户体验. 然后网上百度了一波,发现在UGUI系统上,EventSystem提供了一些方法.那就是EventSyste ...

  4. (转载)C#关于DateTime得到的当前时间的格式和用法

    今天看到工程里有关DateTime的有关知识,之前了解一些用法,比如怎么获取年月日,当前系统时间等等,但是,感觉还是有好多不知道,于是上网搜罗了一下,找到很多有关知识,现在与大家分享下:   Date ...

  5. VisualSFM使用记录1 unable to load libsiftgpu.so

    官网:http://ccwu.me/vsfm/(解决过程蓝色字,问题原因解决方法红色字)SFM computer missing match阶段运行出现错误 More than 189MB of gr ...

  6. 【Visual Studio 扩展工具】如何在ComponentOne的DataTree中实现RightToLeft布局

    概述 C1FlexGrid提供了创建轮廓树的功能,其中可以显示缩进结构,每个节点行旁边都有折叠/展开图标. 然后,用户可以展开和折叠轮廓以查看所需的细节级别. 为此,C1FlexGrid允许您使用其T ...

  7. 【HNOI 2018】道路

    Problem Description \(W\) 国的交通呈一棵树的形状.\(W\) 国一共有\(n - 1\)个城市和\(n\)个乡村,其中城市从\(1\)到\(n - 1\) 编号,乡村从\(1 ...

  8. 【五】jquery之事件(focus事件与blur事件)[提示语的出现及消失时机]

    例题:当鼠标移动到某个文本框时,提示语消失. 当失去焦点时,如果该文本框有内容,保存内容.没有内容,则恢复最初的提示语句 <!DOCTYPE html> <html> < ...

  9. 启动xampp出错,Port 80 in use by "Unable to open process" with PID 4!

    启动xampp出错,Port 80 in use by "Unable to open process" with PID 4! 环境:windows10 80端口被PID为4的应 ...

  10. dom常用操作

    创建节点:document.createElement(元素名), document.createTextNode(文本内容) 添加节点:parent.appendChild(newChild) 移除 ...