手机版
你好,游客 登录 注册
背景:
阅读新闻

C++面试题-链表栈二叉树数据结构

[日期:2013-06-15] 来源:Linux社区  作者:Linux [字体:大 中 小]

5.如何不用递归实现二叉树的前序/后序/中序遍历?

算法思想:三种算法的思想都是让root的Left的Left的Left全都入栈。所以第一个while循环的逻辑,都是相同的。

下面详细分析第2个while循环,这是一个出栈动作,只要栈不为空,就始终要弹出栈顶元素,由于我们之前入栈的都是Left节点,所以每次在出栈的时候,我们都要考虑Right节点是否存在。因为前序/后序/中序遍历顺序的不同,所以在具体的实现上有略为区别。

1)前序遍历

这个是最简单的。

前序遍历是root->root.Left->root.Right的顺序。

因为在第一个while循环中,每次进栈的都可以认为是一个root,所以我们直接打印,然后root.Right和root.Left先后进栈,那么出栈的时候,就能确保先左后右的顺序。

static void PreOrder(BinNode root)

{

Stack stack = new Stack();

BinNode temp = root;

//入栈

while (temp != null)

{

Console.WriteLine(temp.Element);

if (temp.Right != null)

stack.Push(temp.Right);

temp = temp.Left;

}

//出栈,当然也有入栈

while (stack.Count > 0)

{

temp = (BinNode)stack.Pop();

Console.WriteLine(temp.Element);

while (temp != null)

{

if (temp.Right != null)

stack.Push(temp.Right);

temp = temp.Left;

}

}

}

//后序遍历比较麻烦,需要记录上一个访问的节点,然后在本次循环中判断当前节点的Right或Left是否为上个节点,当前节点的Right为null表示没有右节点。

static void PostOrder(BinNode root)

{

Stack stack = new Stack();

BinNode temp = root;

//入栈

while (temp != null)

{

if (temp != null)

stack.Push(temp);

temp = temp.Left;

}

//出栈,当然也有入栈

while (stack.Count > 0)

{

BinNode lastvisit = temp;

temp = (BinNode)stack.Pop();

if (temp.Right == null || temp.Right == lastvisit)

{

Console.WriteLine(temp.Element);

}

else if (temp.Left == lastvisit)

{

stack.Push(temp);

temp = temp.Right;

stack.Push(temp);

while (temp != null)

{

if (temp.Left != null)

stack.Push(temp.Left);

temp = temp.Left;

}

}

}

}

//中序遍历,类似于前序遍历

static void InOrder(BinNode root)

{

Stack stack = new Stack();

BinNode temp = root;

//入栈

while (temp != null)

{

if (temp != null)

stack.Push(temp);

temp = temp.Left;

}

//出栈,当然也有入栈

while (stack.Count > 0)

{

temp = (BinNode)stack.Pop();

Console.WriteLine(temp.Element);

if (temp.Right != null)

{

temp = temp.Right;

stack.Push(temp);

while (temp != null)

{

if (temp.Left != null)

stack.Push(temp.Left);

temp = temp.Left;

}

}

}

}

 

 

6.在二叉树中找出和为某一值的所有路径

算法思想:这道题目的苦恼在于,如果用递归,只能打出一条路径来,其它符合条件的路径打不出来。

为此,我们需要一个Stack,来保存访问过的节点,即在对该节点的递归前让其进栈,对该节点的递归结束后,再让其出栈——深度优先原则(DFS)。

此外,在递归中,如果发现某节点(及其路径)符合条件,如何从头到尾打印是比较头疼的,因为DFS使用的是stack而不是queue,为此我们需要一个临时栈,来辅助打印。

static void FindBinNode(BinNode root, int sum, Stack stack)

{

if (root == null)

return;

stack.Push(root.Element);

//Leaf

if (root.IsLeaf())

{

if (root.Element == sum)

{

Stack tempStack = new Stack();

while (stack.Count > 0)

{

tempStack.Push(stack.Pop());

}

while (tempStack.Count > 0)

{

Console.WriteLine(tempStack.Peek());

stack.Push(tempStack.Pop());

}

Console.WriteLine();

}

}

if (root.Left != null)

FindBinNode(root.Left, sum - root.Element, stack);

if (root.Right != null)

FindBinNode(root.Right, sum - root.Element, stack);

stack.Pop();

}

linux
相关资讯       二叉树  链表栈 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款