二叉树相关概念及其实现
【1】如何判断一颗二叉树是否是搜索二叉树?
【2】如何判断一颗二叉树是完全二叉树?
【3】如何判断一颗二叉树是否是满二叉树?
【4】如何判断一颗二叉树是否是平衡二叉树?(可得出题目套路)
常规实现方法:
【1】常规解法思路:搜索二叉树的特点是左边比中间小,右边比中间大。可以通过中序遍历发现,如果一颗数是搜索二叉树那么遍历出的结果是递增的,可以根据这个特点来判断一颗数是否是二叉树。
public static boolean isSbtCommon(Node node) {
List<Node> list=new ArrayList();
Inorder(node, list);
for(Node node1:list) {
System.out.print(node1.value+" ");
}
System.out.println();
//判断是否是递增
for(int i=0;i<list.size()-1;i++) {
if(list.get(i).value>=list.get(i+1).value) {
return false;
}
}
return true;
}
//中序遍历
public static void Inorder(Node node,List list) {
if(node==null) {
return;
}
Inorder(node.left,list);
list.add(node);//对每次遍历的节点添加到列表中
Inorder(node.right,list);
}
【1】第二种常规解题思路:从横向看,左边小于中间,中间小于右边,所以继续使用中序遍历来解题,我们比较的是左子树的值是否小于节点的值,节点的值是否小于右子树的值。所以我们需要一个变量来记录前一个节点的值,将这个变量与当前节点的值比较来判断是否小于。
//这个值是关键点,每次比较时会进行更新值。这个值可以解释为什么只需要比较一次。
//preValue最开始从最左下节点开始,遇到中点后,从最右下节点开始
public static int preValue=Integer.MIN_VALUE;
public static boolean isSBT(Node node) {
if(node==null) {
return true;
}
boolean left=isSBT(node.left);
//判断左子树是否为搜索二叉树
if(!left) {
return false;
}
if(preValue>=node.value) {
return false;
}else {
preValue=node.value;
}
//如果左子树是搜索二叉树,那么决定是否为搜索二叉树就靠右子树是否是搜索二叉树
return isSBT(node.right);
}
【2】解题思路:完全二叉树定义:要么左右子节点全满,要么只能是左节点满右节点不满,左节点满那么相连兄弟节点不能有子节点。
public static boolean isCbt(Node node) {
if(node==null) {
return false;
}
Queue<Node> queue=new LinkedList();
queue.add(node);
boolean leaf=false;//标记是否左兄弟节点有空,右兄弟节点就不能有节点
while(!queue.isEmpty()) {
Node curNode=queue.poll();
//判断出队节点的左节点为空,右节点不为空
if(curNode.left==null && curNode.right!=null) {
return false;
}
//查看标记,再看兄弟节点是否是存在子节点
if(leaf && (curNode.left!=null || curNode.right!=null)) {
return false;
}
//入队
if(curNode.left!=null) {
queue.add(curNode.left);
}
if(curNode.right!=null) {
queue.add(curNode.right);
}
//标记赋值,判断出队节点的左右子节点是否不存在
if(curNode.left==null || curNode.right==null) {
leaf=true;
}
}
return true;
}
递归套路实现方法:
【1】递归套路解法思路:声明一个对象,这个对象包含最大值,最小值,是否搜索二叉树,这三个变量。每个节点都需要返回这个对象,供父节点判断(比大小)。
//非常规
public static class Result{
boolean is;
int max;
int min;
public Result(boolean is,int max,int min) {
this.is=is;
this.max=max;
this.min=min;
}
}
public static boolean issbt(Node node) {
Result result=postorder(node);
return result.is;
}
public static Result postorder(Node node) {
if(node==null) {
return null;
}
Result r1=postorder(node.left);
Result r2=postorder(node.right);
int max=node.value;
int min=node.value;
boolean is=true;
if(r1!=null) {
max=Math.max(r1.max, max);
min=Math.min(r1.min, min);
}
if(r2!=null) {
max=Math.max(r2.max, max);
min=Math.min(r2.min, min);
}
if(r1!=null && (r1.is || (r1.max>=node.value))) {
is=false;
}
if(r2!=null && (r2.is || (r2.min<=node.value))) {
is=false;
}
return new Result(is,max,min);
}
【3】解题思路:满二叉树定义:节点的个数为2的层数次方减1,所以必须记录层数,与每层个数将其入公式比较。
//套路中常用的对象
public static class Result{
boolean isfbt;
int count;
int height;
public Result(boolean isfbt,int height,int count) {
this.isfbt=isfbt;
this.height=height;
this.count=count;
}
}
public static boolean isfbt(Node node) {
Result result=isFbt(node);
System.out.println(result.count);
if(!result.isfbt) {
return false;
}
return true;
}
public static Result isFbt(Node node) {
if(node==null) {
return new Result(true,0,0);
}
Result l=isFbt(node.left);
Result r=isFbt(node.right);
boolean isfbt=true;
int height=0;
int count=0;
if(!l.isfbt) {
isfbt=false;
}
if(!r.isfbt) {
isfbt=false;
}
height=Math.max(l.height, r.height)+1;
count=l.count+r.count+1;
if(count!=((2<<height-1)-1)) {
isfbt=false;
}
return new Result(isfbt,height,count);
}
【4】套路解法思路:平衡二叉树:左右子树的高度差不大于1。声明一个包含boolean类型的变量,与一个高度的变量。得到左右子树返回的对象时按照要求进行判断。
//套路中常用的对象
public static class Result{
boolean isbbt;
int height;
public Result(boolean isbbt,int height) {
this.isbbt=isbbt;
this.height=height;
}
}
public static boolean isBBT(Node node) {
Result result=isbbt(node);
return result.isbbt;
}
public static Result isbbt(Node node) {
if(node==null) {
return new Result(true,0);
}
Result l=isbbt(node.left);
Result r=isbbt(node.right);
boolean result=true;
int height=0;
//先判断左右子树是不是平衡树
if(!l.isbbt) {
result=false;
}
if(!r.isbbt) {
result=false;
}
//在判断左右子树的高度差
if(!(l.height-r.height>1) && !(l.height-r.height<-1)) {
//高度差的绝对值<=1,选择最高+1的高度为当前节点高度
height=l.height>r.height?l.height+1:r.height+1;
}else {
//高度差绝对值>1
result=false;
}
return new Result(result,height);
}
给定两个二叉树的节点node1和node2,找到他们的最低公共祖先节点。
//视频示例代码
public static Node lowestAncestor(Node head,Node o1,Node o2){
if(head==null || head==o1 || head==o2){
return head;
}
Node left=lowestAncestor(head.left,o1,o2);
Node right=lowestAncestor(head,right,o1,o2);
if(left!=null && right!=null){
return head;
}
//左右两棵树,并不都有返回值
return left!=null?left:right;
}