java实现二叉树

二叉树

什么是二叉树:
二叉树是结点的一个有限集合,该集合或者为空,或者是由一个根节点加上两棵别称为左子树和右子树的二叉树组成。

二叉树的特点
1.每个结点最多有两棵子树,即二叉树不存在度大于2的结点。
2.二叉树为有序树,子树有左右之分,其子树的次序不能颠倒。

二叉树的存储结构
主要为顺序存储链式存储

二叉树的遍历
二叉树的遍历主要分为:前序/中序/后序遍历 三种,多使用递归的思想去实现他的遍历。

**

二叉树相关问题的解决

**

计算二叉树的结点个数

 public static int countTree(TreeNode root){  //递归实现
        if(root == null){			//递归终止条件
            return 0;
        }
        
        int left = countTree(root.left);
        int right = countTree(root.right);
        return  left+ right + 1;			//结点总数 = 左子树 + 右子树 + 根节点(1)
    }

计算叶子结点的个数

public static int leafcount = 0;		//计数变量
    public static int calcLeaf(TreeNode root){
        if (root == null){
            return 0;
        }
        
        //遍历每个节点,判断是否为叶子结点
        calcLeaf(root.left);
        if (root.left == null&&root.right == null){
            leafcount++;
        }
        calcLeaf(root.right);
        return leafcount;
    }

计算二叉树高度
原题链接:https://leetcode-cn.com/problems/maximum-depth-of-binary-tree/

 public static int calcHeight(TreeNode root){
        int left = calcHeight(root.left);
        int right = calcHeight(root.right);
        int heght = Math.max(left,right)+1;//比较左右子树的高度,并输出最大值,再加上根节点的高度(1)。
        return heght;
    }

计算二叉树第K层的节点个数

 public static int calcLevel(TreeNode root, int k){//层层递归
        if (root == null){
            return 0;
        }

        if (k == 1){
            return 1;
        }
        int left = calcLevel(root.left,k-1);
        int right = calcLevel(root.right,k-1);
        return left+right;
    }

查找并输出

public static TreeNode search(TreeNode root,int val){
        if(root == null){
            return null;
        }
        if(root.val == val){
            return root;
        }
        TreeNode left = search(root.left, val);
        if (left!=null){
            return left;
        }
        TreeNode right = search(root.right, val);
       if (right!= null){
           return right;
       }
       return null;
    }

查找是否存在

public static boolean Search(TreeNode root,int val){
        if (root == null){
            return false;
        }
        if (root.val == val){
            return true;
        }
        if (Search(root.left,val)){
            return true;
        }
        return Search(root.right,val);
    }

判断两树是否为镜像树

 public static boolean isMirror(TreeNode p,TreeNode q){
       if (p==null  &&  q==null){
           return true;
       }

       if (p==null||q==null){
           return false;
       }
        return p.val==q.val				//值相等且互为镜像的判断条件
                && isMirror(p.right,q.left)
                && isMirror(p.left,q.right);
     }

判断一棵树是否为对称树
上一题的升级版,这里直接套用上一题的代码
原题链接:https://leetcode-cn.com/problems/symmetric-tree/

 public static boolean isMirror(TreeNode p,TreeNode q){
       if (p==null  &&  q==null){
           return true;
       }

       if (p==null||q==null){
           return false;
       }
        return p.val==q.val
                && isMirror(p.right,q.left)
                && isMirror(p.left,q.right);
     }
     //判断对称树
    public static boolean isSymmetrical(TreeNode root){
        if (isMirror(root.right,root.left)){
            return true;
        }
        return false;
    }

总结

二叉树相关题型都是以二叉树的三种遍历为基本所延伸出来的题型,熟练的运用遍历和递归的思想即可解决绝大部分二叉树的相关问题。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值