1.不构建二叉树,遍历过程中直接添加结果
import java.util.*;
class Main{
static StringBuilder in=new StringBuilder();
static StringBuilder post=new StringBuilder();
static int k;
static char[] ch;
public static void main(String [] args){
Scanner sc=new Scanner(System.in);
String s=sc.next();
ch=s.toCharArray();
dfs();
System.out.println(in);
System.out.println(post);
}
public static void dfs(){
char root =ch[k++];
if(root=='.') return;
dfs();
in.append(root);
dfs();
post.append(root);
}
}
2.构建二叉树,递归方法得到遍历结果
import java.util.*;
class TreeNode{
TreeNode left,right;
char val;
TreeNode(char x){
this.val=x;
}
}
class Main{
static StringBuilder in=new StringBuilder();
static StringBuilder post=new StringBuilder();
static int k;
static char[] ch;
public static void main(String [] args){
Scanner sc=new Scanner(System.in);
String s=sc.next();
ch=s.toCharArray();
TreeNode tree=buildTree();
inorder(tree);
postorder(tree);
System.out.println(in);
System.out.println(post);
}
public static TreeNode buildTree(){
char root=ch[k++];
if(root=='.') return null;
TreeNode node=new TreeNode(root);
node.left=buildTree();
//in.append(root);
node.right=buildTree();
//post.append(root);
return node;
}
//递归写法
public static void inorder(TreeNode node){//中序
if(node==null) return;
inorder(node.left);
in.append(node.val);
inorder(node.right);
}
public static void postorder(TreeNode node){//后序
if(node==null) return;
postorder(node.left);
postorder(node.right);
post.append(node.val);
}
}
3.构建二叉树,非递归方法得到遍历结果
利用栈,后序遍历的结果先求逆后序遍历顺序 即:根 右 左
import java.util.*;
class TreeNode{
TreeNode left,right;
char val;
TreeNode(char x){
this.val=x;
}
}
class Main{
static StringBuilder in=new StringBuilder();
static StringBuilder post=new StringBuilder();
static int k;
static char[] ch;
public static void main(String [] args){
Scanner sc=new Scanner(System.in);
String s=sc.next();
ch=s.toCharArray();
TreeNode tree=buildTree();
inorder(tree);
postorder(tree);
System.out.println(in);
System.out.println(post);
}
public static TreeNode buildTree(){
char root=ch[k++];
if(root=='.') return null;
TreeNode node=new TreeNode(root);
node.left=buildTree();
//in.append(root);
node.right=buildTree();
//post.append(root);
return node;
}
////递归写法
// public static void inorder(TreeNode node){//中序
// if(node==null) return;
// inorder(node.left);
// in.append(node.val);
// inorder(node.right);
// }
// public static void postorder(TreeNode node){//后序
// if(node==null) return;
// postorder(node.left);
// postorder(node.right);
// post.append(node.val);
// }
////非递归写法
public static void inorder(TreeNode node){//中序
Stack<TreeNode> st=new Stack<>();
TreeNode cur=node;
while(cur!=null||!st.isEmpty()){
while(cur!=null){
st.push(cur);
cur=cur.left;
}
if(!st.isEmpty()){
cur=st.pop();
in.append(cur.val);
cur=cur.right;
}
}
}
public static void postorder(TreeNode node){//逆后序 根 右 左
Stack<TreeNode> st=new Stack<>();
TreeNode cur=node;
while(cur!=null||!st.isEmpty()){
while(cur!=null){
st.push(cur);
post.append(cur.val);
cur=cur.right;
}
if(!st.isEmpty()){
cur=st.pop();
cur=cur.left;
}
}
post.reverse();
}
}