二叉树非递归遍历Java实现
2017-06-30 13:59
417 查看
二叉树的前序、中序和后序遍历可以采用递归和非递归的方法实现,递归的方法逻辑简单清晰,易于理解,但递归的方法需要使用额外的栈空间,运行效率较低。而非递归的方法则效率较高。
维基百科上有递归和非递归二叉树三种遍历实现的伪代码,https://en.wikipedia.org/wiki/Tree_traversal#In-order_2
下面是相应的Java实现:
前序遍历(非递归实现):
public static void preOrder(TreeNode x)
{
System.out.println();
if(x!=null)
{
LinkedList<TreeNode> stack=new LinkedList<TreeNode>();
stack.addLast(x);
while(!stack.isEmpty())
{
TreeNode temp=stack.removeLast();
System.out.print(temp.val+" ");
if(temp.rchild!=null)
{
stack.addLast(temp.rchild);
}
if(temp.lchild!=null)
{
stack.addLast(temp.lchild);
}
}
System.out.println();
}
}
中序遍历(非递归实现):
public static void inOrder(TreeNode x)
{
System.out.println();
if(x!=null)
{
LinkedList<TreeNode> stack=new LinkedList<TreeNode>();
while(!stack.isEmpty()||x!=null)
{
if(x!=null)
{
stack.addLast(x);
x=x.lchild;
}
else
{
x=stack.removeLast();
System.out.print(x.val+" ");
x=x.rchild;
}
}
System.out.println();
}
}
后序遍历(非递归实现):
public static void postOrderII(TreeNode x)
{
LinkedList<TreeNode> stack=new LinkedList<TreeNode>();
TreeNode lastNodeVistited=null;
while(!stack.isEmpty()||x!=null)
{
if(x!=null)
{
stack.addLast(x);
x=x.lchild;
}
else
{
TreeNode top=stack.getLast();
if(top.rchild!=null&&lastNodeVistited!=top.rchild)
{
x=top.rchild;
}
else
{
System.out.print(top.val+" ");
lastNodeVistited=stack.removeLast();
}
}
}
System.out.println();
}
维基百科上有递归和非递归二叉树三种遍历实现的伪代码,https://en.wikipedia.org/wiki/Tree_traversal#In-order_2
下面是相应的Java实现:
前序遍历(非递归实现):
public static void preOrder(TreeNode x)
{
System.out.println();
if(x!=null)
{
LinkedList<TreeNode> stack=new LinkedList<TreeNode>();
stack.addLast(x);
while(!stack.isEmpty())
{
TreeNode temp=stack.removeLast();
System.out.print(temp.val+" ");
if(temp.rchild!=null)
{
stack.addLast(temp.rchild);
}
if(temp.lchild!=null)
{
stack.addLast(temp.lchild);
}
}
System.out.println();
}
}
中序遍历(非递归实现):
public static void inOrder(TreeNode x)
{
System.out.println();
if(x!=null)
{
LinkedList<TreeNode> stack=new LinkedList<TreeNode>();
while(!stack.isEmpty()||x!=null)
{
if(x!=null)
{
stack.addLast(x);
x=x.lchild;
}
else
{
x=stack.removeLast();
System.out.print(x.val+" ");
x=x.rchild;
}
}
System.out.println();
}
}
后序遍历(非递归实现):
public static void postOrderII(TreeNode x)
{
LinkedList<TreeNode> stack=new LinkedList<TreeNode>();
TreeNode lastNodeVistited=null;
while(!stack.isEmpty()||x!=null)
{
if(x!=null)
{
stack.addLast(x);
x=x.lchild;
}
else
{
TreeNode top=stack.getLast();
if(top.rchild!=null&&lastNodeVistited!=top.rchild)
{
x=top.rchild;
}
else
{
System.out.print(top.val+" ");
lastNodeVistited=stack.removeLast();
}
}
}
System.out.println();
}
相关文章推荐
- java 实现二叉树深度优先遍历的 前、中、后序遍历(递归)
- 二叉树的前序、中序、后序(递归、非递归)遍历java实现
- 二叉树非递归遍历实现-java版本
- JAVA实现二叉树及递归遍历二叉树
- java语言实现二叉树的前序、中序与后序遍历(递归与非递归)
- Java实现二叉树的遍历(递归和非递归)
- 面试题二: java 实现二叉树的中序优先遍历,不能用递归
- 二叉树非递归遍历----前中后及层序的java实现
- Java 二叉树的前序、中序、后续遍历 递归和迭代实现
- 非递归遍历二叉树--java实现
- 二叉树的递归、非递归及层序遍历的Java实现
- java实现的二叉树(前序、中序、后序)递归和非递归遍历,包含层序遍历
- Java创建二叉树及其遍历的递归和非递归实现
- Java实现-二叉树先序,中序,后序遍历及递归,非递归遍历
- 二叉树非递归遍历(前序,后序),java实现
- 二叉树的遍历 递归非递归 思路和 java实现
- 数据结构二叉树的递归与非递归遍历之java,javascript,php实现可编译(1)java
- 二叉树三种遍历递归及非递归实现(Java)
- java 实现二叉树的构建,先序,中序,后序,层次,递归,非递归的遍历
- java实现二叉树的创建及三种递归遍历