二叉树 java

39 篇文章 0 订阅

二叉树

  • 二叉树的前中后序遍历
public void preOrderTraversal(Node root) {
        if (root == null) {
            return;
        }

        System.out.println(root);
        preOrderTraversal(root.left);
        preOrderTraversal(root.right);

    }              //前序遍历

    public void inOrderTraversal(Node root) {
        if (root == null) {
            return;
        }

        inOrderTraversal(root.left);
        System.out.println(root);
        inOrderTraversal(root.right);
    }              //中序遍历

    public void postOrderTraversal(Node root) {
        if (root == null) {
            return;
        }

        postOrderTraversal(root.left);
        postOrderTraversal(root.right);
        System.out.println(root);
    }               //后序遍历
  • 二叉树的总结点数
 private static int count;

    public int getSize(Node root) {
        if (root == null) {
            return 0;
        }
        count++;
        getSize(root.left);
        getSize(root.right);
        return count;
    }                          //遍历的思想

    public int getSize2(Node root) {
        if (root == null) {
            return 0;
        }
        int left = getSize2(root.left);
        int right = getSize(root.right);
        return left + right + 1;
    }                        //整合的思想



  • 二叉树的叶子结点
    public static int leafSize;

    public int getLeafSize(Node root) {
        if (root == null) {
            return 0;
        }
        if (root.left == null && root.right == null) {
            leafSize++;
        }
        getLeafSize(root.left);
        getLeafSize(root.right);
        return leafSize;
    }                               //遍历的思想

    public int leafSize(Node root) {
        if (root == null) {
            return 0;
        }
        if (root.left == null && root.right == null) {
            return 1;
        }
        int left = leafSize(root.left);
        int right = leafSize(root.right);
        return left + right;
    }                           //整合的思想
  • 求二叉树的高度(深度)
 public int getHeight(Node root) {
        if (root == null) {
            return 0;
        }
        int left = getHeight(root.left);
        int right = getHeight(root.right);
        return Math.max(left, right) + 1;
    }
  • 求二叉树第k层的结点数
 public int getKLevel(Node root, int k) {
   if(root==null){
       return 0;
   }
   if(k==1){
       return 1;
   }

        int left=getKLevel(root.left,k-1);
        int right=getKLevel(root.right,k-1);
        return left+right;

    }
  • 找到值为val的结点
 public Node find(Node root, int val){
        if(root==null){
            return null;
        }
        if(root.value==val){
            return root;
        }
        Node n=find(root.left,val);
        if(n!=null){
            return n;
        }
        Node m=find(root.right,val);
        if(m!=null){
            return m;
        }
      return null;
    }                                                //返回这个结点
     public  boolean  find1 (Node root,int val){
        if(root==null){
            return false;
        }
        if(root.value==val){
            return true;
        }
        if(find1(root.left,val))
        {
            return true;
        }
        if(find1(root.right,val))
        {
            return true;
        }
        return false;

    }                                     //如果找到,返回true,否则返回false
  • 给定两个二叉树,编写一个函数来检验它们是否相同。

如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/subtree-of-another-tree
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

class Solution {
    public boolean isSameTree(TreeNode p, TreeNode q) {
        if(p==null&&q==null){
            return true;
        }
        if(p==null||q==null){
            return false;
        }
        return  p.val==q.val
            &&  isSameTree(p.left,q.left)
            &&  isSameTree(p.right,q.right);
        
    }
}
  • 给定两个非空二叉树 s 和 t,检验 s 中是否包含和 t 具有相同结构和节点值的子树。s 的一个子树包括 s 的一个节点和这个节点的所有子孙。s 也可以看做它自身的一棵子树。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/subtree-of-another-tree
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

class Solution {
      public boolean isSameTree(TreeNode p, TreeNode q) {
          if(p==null&&q==null){
              return true;
          }
          if(p==null||q==null){
              return false;
          }
          return p.val==q.val
              && p.left==q.left
              &&p.right==q.right;
      
        
    }
    public boolean search(TreeNode root,TreeNode t){
        if(root==null){
            return false;
        }
        if(isSameTree(root,t)){
            return true;
        }
        if(isSubTree(root.left,t)){
            return true;
        }
        if(isSubTree(root.right,t)){
            return true;
        }
        return false;
       
        
    }
    public boolean isSubtree(TreeNode s, TreeNode t) {
        
       return search(s,t);
        
    }
}
  • 给定一个二叉树,判断它是否是高度平衡的二叉树。
class Solution {

    public int getHeight(TreeNode root){
        if(root==null){
            return 0;
        }
        int left=getHeight(root.left);
        int right=getHeight(root.right);
        return Math.max(left,right)+1;
        
    }
    
    public boolean isBalanced(TreeNode root) {
        if(root==null){
            return true;
        }
        if(!(isBalanced(root.left))){
            return false;
        }
        if(!(isBalanced(root.right))){
            return false;
        }
           int leftH=getHeight(root.left);
           int rightH=getHeight(root.right);
           int diff=leftH-rightH;
           if(diff>=-1&&diff<=1){
               return true;
           } 
        return false;
}
}
  • 给定一个二叉树,检查它是否是镜像对称的。
class Solution {
    public boolean isMirrorTree(TreeNode p,TreeNode q){
        if(p==null&&q==null){
            return true;
        }
        if(p==null||q==null){
            return false;
        }
        return p.val==q.val
                &&   isMirrorTree(p.left,q.right)
                &&   isMirrorTree(p.right,q.left);
    }
    public boolean isSymmetric(TreeNode root) {
        if(root==null){
            return true;
        }
        return isMirrorTree(root.left,root.right);
        
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值