建立二叉树的两种方式
(1)根据前序遍历数组和后序遍历数组建立
(2)根据中序遍历数组和后序遍历数组建立
遍历这里实现了后序遍历和层序遍历,中序和前序只是list.add的位置发生了改变(这里是递归实现)
这里list集合存储了遍历的集合元素
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
class Node {
int val;
Node left;
Node right;
public Node(int val){
this.val = val;
}
@Override
public String toString() {
return String.format("%c ",this.val);
}
}
public class Main {
public static Node buildTree1(int[] preorder, int[] inorder) {
if(preorder.length == 0){
return null;
}
int rootValues = preorder[0];
Node root = new Node(rootValues);
int leftCount = 0;
for(leftCount = 0;leftCount < inorder.length;leftCount++){
if(inorder[leftCount] == rootValues){
break;
}
}
int[] leftPreorder = Arrays.copyOfRange(preorder,1,leftCount+1);
int[] leftInorder = Arrays.copyOfRange(inorder,0,leftCount);
root.left = buildTree(leftPreorder,leftInorder);
int[] rightPreorder = Arrays.copyOfRange(preorder,leftCount+1,preorder.length);
int[] rightInorder = Arrays.copyOfRange(inorder,leftCount+1,inorder.length);
root.right = buildTree1(rightPreorder,rightInorder);
return root;
}
public static Node buildTree(int[] inorder, int[] postorder) {
if(inorder.length == 0){
return null;
}
int rootValues = postorder[postorder.length-1];
Node root = new Node(rootValues);
int leftCount = 0;
for(leftCount = 0;leftCount < inorder.length;leftCount++){
if(inorder[leftCount] == rootValues){
break;
}
}
int[] leftInorder = Arrays.copyOfRange(inorder,0,leftCount);
int[] leftPostorder = Arrays.copyOfRange(postorder,0,leftCount);
root.left = buildTree(leftInorder,leftPostorder);
int[] rightInorder = Arrays.copyOfRange(inorder,leftCount+1,inorder.length);
int[] rightPostorder = Arrays.copyOfRange(postorder,leftCount,inorder.length-1);
root.right = buildTree(rightInorder,rightPostorder);
return root;
}
public boolean search(Node root,Node t){
if(root == null){
return false;
}
if(root == t){
return true;
}
if(search(root.left,t)){
return true;
}
return search(root.right,t);
}
public Node lowestCommonAncestor(Node root, Node p, Node q) {
if(p == root || q == root){
return root;
}
boolean pInleft = search(root.left,p);
boolean qInleft = search(root.left,q);
if(pInleft && qInleft){
return lowestCommonAncestor(root.left,p,q);
}
if(!pInleft && !qInleft){
return lowestCommonAncestor(root.right,p,q);
}
return root;
}
private static List<Integer> list = new ArrayList<>();
public static List<Integer> postOrderTraversal(Node root){
if(root == null){
return new ArrayList<>();
}
postOrderTraversal(root.left);
postOrderTraversal(root.right);
list.add(root.val);
return list;
}
public static void levelOrder(Node root){
if(root != null){
return;
}
Queue<Node> que = new LinkedList<>();
que.offer(root);
while(!que.isEmpty()){
Node front = que.poll();
System.out.println(front.val);
if(front.left != null){
que.offer(front.left);
}
if(front.right != null){
que.offer(front.right);
}
}
}
public static List<Integer> postOrderTraversal2(Node root){
List<Integer> result = new ArrayList<>();
if(root == null){
return new ArrayList<>();
}
List<Integer> left = postOrderTraversal2(root.left);
List<Integer> right = postOrderTraversal2(root.right);
result.addAll(left);
result.addAll(right);
result.add(root.val);
return result;
}
public static boolean isSameTree(Node s,Node t){
if(s == null && t == null){
return true;
}
if(s == null || t == null){
return false;
}
if(s.val == t.val && isSameTree(s.left,t.left) && isSameTree(s.right,t.right)){
return true;
}
return false;
}
public static boolean isSubTree(Node s,Node t){
if(isSameTree(s.left,t) || isSameTree(s.right,t) || isSameTree(s,t)){
return true;
}
return false;
}
}