1.二叉树常用四种查找方法
import java.util.*;
public class BinaryTree {
public static class Node {
public char val;
public Node left = null;
public Node right = null;
private Node(char val) {
this.val = val;
}
}
}
public class TreeSearch {
//查找树中是否包含值为val的结点 返回引用
BinaryTree.Node search1(BinaryTree.Node root, int val) {
if (root == null) {
return null;
}
if (root.val == val) {
return root;
}
BinaryTree.Node left = search1(root.left, val);
if (left != null) {//找不到为空 找到了则不为空
return left;//找到了
}
return search1(root.right, val);
}
//查找树中是否包含值为val的结点 返回boolean类型
boolean search2(BinaryTree.Node root, int val) {
if (root == null) {
return false;
}
if (root.val == val) {
return true;
}
if (search2(root.left, val)) { //没找到false 找到则为true
return true;
}
return search2(root.right, val);
}
//subRoot判断是否为root的子树
boolean search3(BinaryTree.Node root, BinaryTree.Node subRoot) {
if (root == null) {
return false;
}
if (isSameTree(root, subRoot)) {
return true;
}
if (search3(root.left, subRoot)) {
return true;
}
return search3(root.right, subRoot);
}
public boolean isSameTree(Node p, Node q) {
//都是空树肯定相同
if (p == null && q == null) {
return true;
}
//如果其中有一个为空则不是相同的树
// if(p==null||q==null){ return false;}
if (p == null) {
return false;
}
if (q == null) {
return false;
}
return p.val == q.val
&& isSameTree(p.left, q.left)
&& isSameTree(p.right, q.right);
}
//结点node是树的左子树还是右子树?
boolean search4(BinaryTree.Node root, BinaryTree.Node node) {
if (root == null) {
return false;
}
if (root == node) {
return true;
}
if (search4(root.left, node)) {
return true;
}
return search4(root.right, node);
}
}
2.还原二叉树
2.1已知前序+中序 或 中序+后序
1)前序+中序 = 唯一二叉树
//buildTree(传入数组)、buildTree3(传入泛型List< Integer > )
2)中序+后序 = 唯一二叉树
//buildTree2
import java.util.Arrays;
import java.util.TreeSet;
public class Solution {
private static class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
}
public TreeNode buildTree(int[] preorder, int[] inorder) {//前序+中序
if (preorder.length == 0) {
return null;
}
int rootValue = preorder[0];
int leftCount; //根节点在中序中的下标
for (leftCount = 0; leftCount < inorder.length; leftCount++) {
if (inorder[leftCount] == rootValue) {
break;
}
}
TreeNode root = new TreeNode(rootValue);
// 1.复原左子树
int[] leftPreorder = Arrays.copyOfRange(preorder,
1, 1 + leftCount); // 左子树的前序
int[] leftInorder = Arrays.copyOfRange(inorder,
0, leftCount); // 左子树的中序
root.left = buildTree(leftPreorder, leftInorder);
// 2.复原右子树
int[] rightPreorder = Arrays.copyOfRange(preorder,
1 + leftCount, preorder.length); // 右子树的前序
int[] rightInorder = Arrays.copyOfRange(inorder,
leftCount + 1, inorder.length); // 右子树的中序
root.right = buildTree(rightPreorder, rightInorder);
return root;
}
public TreeNode buildTree2(int[] i, int[] p) {//中序+后序
if (i.length == 0) {
return null;
}
int r = p[p.length - 1];
int lc = indexOf(i, r); // 根节点在中序中的下标
TreeNode root = new TreeNode(r);
int[] li = Arrays.copyOfRange(i, 0, lc); //左子树的中序
int[] lp = Arrays.copyOfRange(p, 0, lc); //左子树的后序
root.left = buildTree2(li, lp);
int[] ri = Arrays.copyOfRange(i, lc + 1, i.length); //右子树的中序
int[] rp = Arrays.copyOfRange(p, lc, p.length - 1); //右子树的后序
root.right = buildTree2(ri, rp);
return root;
}
private int indexOf(int[] a, int r) {
for (int i = 0; i < a.length; i++) {
if (a[i] == r) {
return i;
}
}
return -1;
}
public TreeNode buildTree3(List<Integer> preorder, List<Integer> inorder) { //前序+中序
if (preorder.isEmpty()) {
return null;
}
int rootValue = preorder.get(0); //得到根的值
int leftCount = inorder.indexOf(rootValue); //在中序遍历中查找下标
List<Integer> leftPre = preorder.subList(1, 1 + leftCount); //分割
List<Integer> leftIn = inorder.subList(0, leftCount); //subList[)
List<Integer> rightPre = preorder.subList(1 + leftCount, preorder.size());
List<Integer> rightIn = inorder.subList(1 + leftCount, preorder.size());
TreeNode root = new TreeNode(rootValue);
root.left = buildTree3(leftPre, leftIn);
root.right = buildTree3(rightPre, rightIn);
return root;
}
}
注意:每发生一次调用,数组就会拷贝一次,影响时间复杂度,效率比较低。
改进:不需要额外拷贝数组,直接用下标来控制,传入前序、中序开始和结束的下标,提高性能。
TreeNode buildTree4(int[] preorder, int preFrom, int preTo, int[] inorder, int inFrom, int inTo) {
//……
}
2.2只有前序(带空结点)遍历创建二叉树
前序:A BD##E#H## CF##G##
1)无法直接切割为左右子树序列
2)在创建树的过程中,会得到用掉了多少结点
3)返回树的根节点,返回用掉的结点数
4)递归出口1:size()==0 直接返回null、used=0
5)递归出口2:遇到#,返回null、used=1
public class Solution {
private static class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
}
//定义一个包装类BSTRV
private static class BTRV {
private TreeNode root; //创建好树的根节点
private int used; //使用结点数
}
BTRV buildTree5(List<Integer> preorder) {
BTRV returnValue = new BTRV(); //返回值也必需是BTRV类型
if (preorder.size() == 0) { //递归出口1
returnValue.root = null;
returnValue.used = 0;
return returnValue;
}
int rootValue = preorder.get(0); //前序遍历找到根的值
if (rootValue == '#') { //遇到#为空树 递归出口2
returnValue.root = null;
returnValue.used = 1;
return returnValue;
}
BTRV leftReturn = buildTree5(preorder.subList(1, preorder.size())); //左子树序列
//不清楚到哪截至,但知道左子树序列从1开始
BTRV rightReturn = buildTree5(preorder.subList(1 + leftReturn.used, preorder.size()));//创建右子树
//此时已使用结点数=1+左子数使用个数
TreeNode root = new TreeNode(rootValue); //创建根
root.left = leftReturn.root;//根的左子树
root.right = rightReturn.root;//根的右子树
returnValue.root = root; //返回值的root是创建好的root
returnValue.used = 1 + leftReturn.used + rightReturn.used;
//返回值的used是使用掉的结点数=根1+左子数使用结点数+右子树使用结点数
return returnValue; //返回 returnValue
}
}