VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > temp > 简明python教程 >
  • 二叉树遍历(2)

public void PostOrderTraversalWithoutRecusion2(Action<BinaryTree<T>, int> action) { Stack<NodeWrapper> stack = new Stack<NodeWrapper>(); NodeWrapper wrapper = new NodeWrapper(this, false); stack.Push(wrapper); while (stack.Count > 0) { wrapper = stack.Pop(); if (wrapper.Node.Left != null && !wrapper.FromLeft) { stack.Push(wrapper); stack.Push(new NodeWrapper(wrapper.Node.Left, false)); } else { if(stack.Count > 0) stack.Peek().FromLeft = true; if (wrapper.Node.Right != null && !wrapper.FromRight) { wrapper.FromRight = true; stack.Push(wrapper); stack.Push(new NodeWrapper(wrapper.Node.Right, false)); } else action(wrapper.Node, 1); } } }

测试代码类似非递归版本, 这里为了节省篇幅就不贴了;


相关教程