通过前序遍历和后序遍历来构造出后序遍历/树
思路:由前序遍历可以看出树根是A,对应中序遍历,可以看出左子树有4个元素为DBGE,再把这四个元素对应到前序遍历中可以得知B是左子树的树跟……
左子树前序:BDEG
左子树中序:DBGE
由此可见,规模不断缩小,可用递归推出这棵树。
思考算法时:只要减小问题规模即可
初始值的确定:根据递归函数定义
代码示例:
package com.sise.recursion;
/*
* 遍历
*/
public class TreeTraversal {
/*
* 前序遍历
*/
public void preOrder(TreeNode root) {
if(root==null){
return;
}
System.out.print(root.getValue());
preOrder(root.getLeft());
preOrder(root.getRighe());
}
/*
* 中序遍历
*/
public void inOrder(TreeNode root) {
if(root==null){
return;
}
inOrder(root.getLeft());
System.out.print(root.getValue());
inOrder(root.getRighe());
}
/*
* 后序遍历
*/
public void postOrder(TreeNode root) {
if(root==null){
return;
}
postOrder(root.getLeft());
postOrder(root.getRighe());
System.out.print(root.getValue());
}
/*
* 直接返回后序遍历,不建树
*/
public String postOrder(String preOrder,String inOrder) {
if(preOrder.isEmpty()){//判断特殊情况
// return null;返回的应该是字符串
return "";
}
char rootValue=preOrder.charAt(0);
int rootIndex=inOrder.indexOf(rootValue);
return
postOrder(
preOrder.substring(1,1+rootIndex),
inOrder.substring(0,rootIndex))+
postOrder(
preOrder.substring(1+rootIndex),
inOrder.substring(1+rootIndex))+
rootValue;
}
public static void main(String[] args) {
TreeCreator creator=new TreeCreator();
TreeTraversal traversal=new TreeTraversal();
// TreeNode sampleTree=creator.createSampleTree();
// System.out.print("前序:");
// traversal.preOrder(sampleTree);
// System.out.println();
// System.out.print("中序:");
// traversal.inOrder(sampleTree);
// System.out.println();
// System.out.print("后序:");
// traversal.postOrder(sampleTree);
// System.out.println();
System.out.println("-----构造树再遍历出后序--------");
TreeNode tree=creator.creataTree("ABDEGCF", "DBGEACF");
traversal.postOrder(tree);
System.out.println();
traversal.postOrder(creator.creataTree("", ""));
System.out.println();
traversal.postOrder(creator.creataTree("A", "A"));
System.out.println();
traversal.postOrder(creator.creataTree("AB", "BA"));
System.out.println();
System.out.println("-----直接构造后序--------");
System.out.println(
traversal.postOrder("ABDEGCF", "DBGEACF"));
System.out.println(
traversal.postOrder("", ""));
System.out.println(
traversal.postOrder("A", "A"));
System.out.println(
traversal.postOrder("AB", "BA"));
}
}
package com.sise.recursion;
/*
* 建树
*/
public class TreeCreator {
// public TreeNode createSampleTree() {
// TreeNode root=new TreeNode('A');
// root.setLeft(new TreeNode('B'));
// root.getLeft().setLeft(new TreeNode('D'));
// root.getLeft().setRighe(new TreeNode('E'));
// root.getLeft().getRighe().setLeft(new TreeNode('G'));
// root.setRighe(new TreeNode('C'));
// root.getRighe().setRighe(new TreeNode('F'));
// return root;
// }
public TreeNode creataTree(String preOrder,String inOrder) {
if(preOrder.isEmpty()){//判断特殊情况
return null;
}
char rootValue=preOrder.charAt(0);
int rootIndex=inOrder.indexOf(rootValue);
TreeNode root=new TreeNode(rootValue);
root.setLeft(
creataTree(
preOrder.substring(1,1+rootIndex),
inOrder.substring(0,rootIndex)));
root.setRighe(
creataTree(
preOrder.substring(1+rootIndex),
inOrder.substring(1+rootIndex)));
return root;
}
}
package com.sise.recursion;
public class TreeNode {
private final char value;
private TreeNode left;
private TreeNode righe;
public TreeNode (char value) {
this.value=value;
this.left=null;
this.righe=null;
}
public TreeNode getLeft() {
return left;
}
public void setLeft(TreeNode left) {
this.left = left;
}
public TreeNode getRighe() {
return righe;
}
public void setRighe(TreeNode righe) {
this.righe = righe;
}
public char getValue() {
return value;
}
}
运行结果: