二叉树 非递归遍历

  1. /**
  2. * 非递归 中序遍历
  3. * **/
  4. public void InOrderTreeWalk(BSTreeNode<T> root, Action<BSTreeNode<T>> func) {
  5. if (root == null) {
  6. return;
  7. }
  8. Stack<BSTreeNode<T>> stack = new Stack<BSTreeNode<T>>((int)(2 * Math.Log(Count + 1)));
  9. BSTreeNode<T> current = root;
  10. while (current != null) {
  11. stack.Push(current);
  12. current = current.left;
  13. }
  14. while (stack.Count != 0) {
  15. current = stack.Pop();
  16. func(current);
  17. BSTreeNode<T> node = current.right;
  18. while (node != null) {
  19. stack.Push(node);
  20. node = node.left;
  21. }
  22. }
  23. }





posted @ 2017-04-11 23:50  xiejunzhao  阅读(287)  评论(0编辑  收藏  举报