一.先序中序建树
image.png思路:根据先序遍历数组的元素从左到右确定根结点,先构建左子树后构建右子树。
中序遍历数组确定左右子树以及左右子树的节点个数。
class TreeNode{
public int val;
public TreeNode left = null;
public TreeNode right = null;
TreeNode(int val){
this.val = val;
}
}
public class Solution {
public TreeNode recreateTree(int [] pre,int [] in, int preBegin, int preEnd,
int inBegin, int inEnd){
if (preBegin > preEnd){return null;}
TreeNode root = new TreeNode(pre[preBegin]);
int k = inBegin;
while (in[k] != pre[preBegin]){
k++;
}
root.left = recreateTree(pre, in, preBegin + 1, preBegin + k - inBegin, inBegin, k - 1);
root.right = recreateTree(pre, in, preEnd - inEnd + k + 1, preEnd, k + 1, inEnd);
return root;
}
public void printIn(TreeNode root){
if (root == null){return;}
printIn(root.left);
System.out.print(root.val + " ");
printIn(root.right);
}
public void printPost(TreeNode root){
if (root == null){return;}
printPost(root.left);
printPost(root.right);
System.out.print(root.val + " ");
}
public void printPre(TreeNode root){
if (root == null){return;}
System.out.print(root.val + " ");
printPre(root.left);
printPre(root.right);
}
public static void main(String[] args){
int[] pre = {1, 3, 6, 7, 5, 8, 4};
int[] in = {6, 3, 7, 1, 8, 4, 5};
TreeNode root = new Solution().recreateTree(pre, in, 0, 6, 0, 6);
System.out.print("先序遍历:");
new Solution().printPre(root);
System.out.println();
System.out.print("中序遍历:");
new Solution().printIn(root);
System.out.println();
System.out.print("后序遍历:");
new Solution().printPost(root);
System.out.println();
}
}
image.png
二.后序中序建树
image.png思路:根据后序遍历数组的元素从右到左确定根结点,并且先构建右子树,后构建左子树。
中序遍历数组确定左右子树以及左右子树的节点个数。
class TreeNode{
int val;
TreeNode left = null;
TreeNode right = null;
TreeNode(int val){
this.val = val;
}
}
public class Solution {
public TreeNode createTree(int[] in, int[] post, int inBegin, int inEnd, int postBegin, int postEnd){
if (postEnd < postBegin){return null;}
int k = inBegin;
while (in[k] != post[postEnd]){
k++;
}
TreeNode root = new TreeNode(post[postEnd]); //k - inBegin
root.right = createTree(in, post, k + 1, inEnd, postEnd + k - inEnd, postEnd-1);
root.left = createTree(in, post, inBegin, k - 1, postBegin, postBegin + k - inBegin - 1);
return root;
}
public void printIn(TreeNode root){
if (root == null){return;}
printIn(root.left);
System.out.print(root.val + " ");
printIn(root.right);
}
public void printPost(TreeNode root){
if (root == null){return;}
printPost(root.left);
printPost(root.right);
System.out.print(root.val + " ");
}
public void printPre(TreeNode root){
if (root == null){return;}
System.out.print(root.val + " ");
printPre(root.left);
printPre(root.right);
}
public static void main(String[] args){
int[] in = {6, 3, 7, 1, 8, 4, 5};
int[] post = {6, 7, 3, 4, 8, 5, 1};
TreeNode root = new Solution().createTree(in, post, 0, 6, 0, 6);
System.out.print("先序遍历:");
new Solution().printPre(root);
System.out.println();
System.out.print("中序遍历:");
new Solution().printIn(root);
System.out.println();
System.out.print("后序遍历:");
new Solution().printPost(root);
System.out.println();
}
}
image.png
网友评论