acm模式实现树的遍历
Posted 王六六同学
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了acm模式实现树的遍历相关的知识,希望对你有一定的参考价值。
今天的笔试题,acm模式写树,第一次遇到,关于树的输入输出格式做一个记录。
题目描述
分别按照二叉树先序,中序和后序打印所有的节点。
输入描述:
第一行输入两个整数 n 和 root,n 表示二叉树的总节点个数,root 表示二叉树的根节点。
以下 n 行每行三个整数 fa,lch,rch,表示 fa 的左儿子为 lch,右儿子为 rch。(如果 lch 为 0 则表示 fa 没有左儿子,rch同理)
输出描述:
输出三行,分别表示二叉树的先序,中序和后序。
递归:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* @author WanZi
* @create 2022-10-11 16:33
*/
//二叉树的前序、中序、后序、遍历
public class Tree
public static StringBuilder pre = new StringBuilder();
public static StringBuilder in = new StringBuilder();
public static StringBuilder post = new StringBuilder();
public static void main(String[] args) throws IOException
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
reader.readLine();
TreeNode root = creat(reader);
preOrder(root);
inOrder(root);
postOrder(root);
System.out.println();
System.out.println("前序:" + pre.toString().trim());
System.out.println("中序:" + in.toString().trim());
System.out.println("后序:" + post.toString().trim());
public static TreeNode creat(BufferedReader reader)
String[] s = new String[0];
try
s = reader.readLine().trim().split(" ");
int val = Integer.parseInt(s[0]);
int left = Integer.parseInt(s[1]);
int right = Integer.parseInt(s[2]);
TreeNode root = new TreeNode(val);
if(left != 0)
root.left = creat(reader);
if(right != 0)
root.right = creat(reader);
return root;
catch (IOException e)
return null;
public static void preOrder(TreeNode root)
if(root == null)
return;
pre.append(root.val + " ");
preOrder(root.left);
preOrder(root.right);
public static void inOrder(TreeNode root)
if(root == null)
return;
inOrder(root.left);
in.append(root.val + " ");
inOrder(root.right);
public static void postOrder(TreeNode root)
if(root == null)
return;
postOrder(root.left);
postOrder(root.right);
post.append(root.val + " ");
class TreeNode
int val;
TreeNode left;
TreeNode right;
public TreeNode(int val)
this.val = val;
非递归:
//前序
public static void preOrder(TreeNode root)
if(root==null) return ;
Stack<TreeNode> s = new Stack<>();
TreeNode p = root;
while(p!=null||!s.isEmpty())
while(p!=null)
pre.append(p.val+" ");
s.push(p);
p = p.left;
p = s.pop();
p = p.right;
public static void preOrder(TreeNode root)
if(root==null) return ;
Stack<TreeNode> s = new Stack<>();
s.push(root);
while(!s.isEmpty())
root = s.pop();
pre.append(root.val+" ");
if(root.right!=null)
s.push(root.right);
if(root.left!=null)
s.push(root.left);
//中序
public static void inOrder(TreeNode root)
if(root==null) return ;
Stack<TreeNode> s = new Stack<>();
TreeNode p = root;
while(p!=null||!s.isEmpty())
while(p!=null)
s.push(p);
p = p.left;
p = s.pop();
in.append(p.val+" ");
p = p.right;
//后序
public static void postOrder(TreeNode root)
if(root==null) return ;
Stack<TreeNode> s1 = new Stack<>();
Stack<TreeNode> s2 = new Stack<>();
s1.push(root);
while(!s1.isEmpty())
root = s1.pop();
s2.push(root);
if(root.left!=null)
s1.push(root.left);
if(root.right!=null)
s1.push(root.right);
while(!s2.isEmpty())
post.append(s2.pop().val+" ");
public static void postOrder(TreeNode root)
if(root==null) return ;
Stack<TreeNode> s = new Stack<>();
TreeNode c = null;
s.push(root);
while(!s.isEmpty())
c = s.peek();
if(c.left!=null&&c.left!=root&&c.right!=root)
s.push(c.left);
else if(c.right!=null&&c.right!=root)
s.push(c.right);
else
c = s.pop();
post.append(c.val+" ");
root = c;
以上是关于acm模式实现树的遍历的主要内容,如果未能解决你的问题,请参考以下文章