二叉树的基本操作2

目录

判断两颗树是否相同

判断一颗树是否为另一棵树的子树 

求给定的二叉树的深度

判断一棵树是否为平衡二叉树

判断两棵树是否为对称二叉树

层序遍历二叉树

判断完全二叉树


判断两颗树是否相同


给定两个二叉树,编写一个函数来检验它们是否相同。

如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的。

示例 1:

输入:       1         1
          / \       / \
         2   3     2   3

        [1,2,3],   [1,2,3]

输出: true

示例 2:

输入:      1          1
          /           \
         2             2

        [1,2],     [1,null,2]

输出: false


示例 3:

输入:       1         1
          / \       / \
         2   1     1   2

        [1,2,1],   [1,1,2]

输出: false

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


解题:

  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 也可以看做它自身的一棵子树。

示例 1:
给定的树 s:

     3
    / \
   4   5
  / \
 1   2


给定的树 t:

   4 
  / \
 1   2


返回 true,因为 t 与 s 的一个子树拥有相同的结构和节点值。

示例 2:
给定的树 s:

     3
    / \
   4   5
  / \
 1   2
    /
   0


给定的树 t:

   4
  / \
 1   2


返回 false。

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


解题:

在解该题时,需要用到上面的isSameTree()

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);
    }
    public boolean isSubtree(TreeNode s, TreeNode t) {
        if(s == null && t == null){//两棵树都为空,认为t是s的子树
            return true;
        }
        if(s == null || t == null){//一颗为空,另一颗不为空,显然t不是s的子树
            return false;
        }
        if (s.val == t.val) {//跟结点相同时,判断两颗树是否相同,t是否为s的左子树,或t是否为s的右子树
            return isSameTree(s,t)
                 ||isSubtree(s.left,t)
                 ||isSubtree(s.right,t);
        } else { //当跟结点不相同时,只考虑t是否为s的左子树,或t是否为s的右子树,不再考虑两颗树相同的情况
            return isSubtree(s.left,t)
                  ||isSubtree(s.right,t);
        }
             
    }
}


求给定的二叉树的深度

给定一个二叉树,找出其最大深度。

二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。

说明: 叶子节点是指没有子节点的节点。

示例:
给定二叉树 [3,9,20,null,null,15,7],

    3
   / \
  9  20
    /  \
   15   7

返回它的最大深度 3 。

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


解题:

class Solution {
    public int maxDepth(TreeNode root) {
        if(root == null){//空树的深度为0
            return 0;
        }
        if(root.left == null && root.right == null){//只有跟结点时,树的最大深度为1
            return 1;
        }
        int leftDepth = maxDepth(root.left);//左子树的最大深度
        int rightDepth = maxDepth(root.right);//右子树的最大深度
        return 1 + (leftDepth > rightDepth?leftDepth:rightDepth);//跟结点深度 + max(左子树最大深度,右子树最大深度)
    }
}

这道题居然被绊倒在三目运算符的格式上,写了好几次都不正确。

基础果然很重要。 

 


判断一棵树是否为平衡二叉树

给定一个二叉树,判断它是否是高度平衡的二叉树。

本题中,一棵高度平衡二叉树定义为:


一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过1。(关键!!)


示例 1:

给定二叉树 [3,9,20,null,null,15,7]

    3
   / \
  9  20
    /  \
   15   7

返回 true 。

示例 2:

给定二叉树 [1,2,2,3,3,null,null,4,4]

       1
      / \
     2   2
    / \
   3   3
  / \
 4   4


返回 false 。

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


解题:

解这道题,首先需要maxDepth()的帮助来计算左右子树的深度,以此算出左右子树的差值

class Solution {
     public int maxDepth(TreeNode root) {
        if(root == null){//空树的深度为0
            return 0;
        }
        if(root.left == null && root.right == null){//只有跟结点时,树的最大深度为1
            return 1;
        }
        int leftDepth = maxDepth(root.left);//左子树的最大深度
        int rightDepth = maxDepth(root.right);//右子树的最大深度
        return 1 + (leftDepth > rightDepth?leftDepth:rightDepth);//跟结点深度 + max(左子树最大深度,右子树最大深度)
    }
    public boolean isBalanced(TreeNode root) {
        if(root == null){//空树算平衡二叉树
          return true;
        }
        if(root.left == null && root.right == null){//只有一个跟结点,算平衡二叉树
            return true;
        }
        int leftDepth = maxDepth(root.left);
        int rightDepth =  maxDepth(root.right);
        if(leftDepth - rightDepth > 1|| rightDepth - leftDepth > 1){//左右两棵子树的差值 > 1,则不是平衡二叉树
                 return false;
             }
        return isBalanced(root.left) && isBalanced(root.right);
    }
}

在这道题中,递归的地方还是很模糊的。

最初的错误代码

class Solution {
     public int maxDepth(TreeNode root) {
        if(root == null){//空树的深度为0
            return 0;
        }
        if(root.left == null && root.right == null){//只有跟结点时,树的最大深度为1
            return 1;
        }
        int leftDepth = maxDepth(root.left);//左子树的最大深度
        int rightDepth = maxDepth(root.right);//右子树的最大深度
        return 1 + (leftDepth > rightDepth?leftDepth:rightDepth);//跟结点深度 + max(左子树最大深度,右子树最大深度)
    }
    public boolean isBalanced(TreeNode root) {
        if(root == null){//空树算平衡二叉树
          return true;
        }
        int leftDepth = maxDepth(root.left);//左子树的最大深度
        int rightDepth = maxDepth(root.right);//右子树的最大深度
        if(leftDepth - rightDepth > 1 || rightDepth - leftDepth > 1){//左右两棵子树的差值 > 1,则不是平衡二叉树
            return false;
        } 
            return true;
    }
}

错误原因:忽略了题目中的每个节点只判断了跟结点,上面的错误代码是无法通过以下输入的,画出这棵树之后,才关注到每个节点

 


判断两棵树是否为对称二叉树

给定一个二叉树,检查它是否是镜像对称的。

例如,二叉树 [1,2,2,3,4,4,3] 是对称的。

    1
   / \
  2   2
 / \ / \
3  4 4  3


但是下面这个 [1,2,2,null,3,null,3] 则不是镜像对称的:

    1
   / \
  2   2
   \   \
   3    3

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


解题(递归方法):

class Solution {
    public boolean isSymmetric(TreeNode root) {
        if(root == null){//空树 返回true
            return true;        
        }
        return isMirror(root.left,root.right);//判断左右子树是否为镜像关系
    }
    public boolean isMirror(TreeNode t1, TreeNode t2){
        if(t1 == null && t2 == null){//只有跟结点,是对称二叉树
            return true;
        }
        if(t1 == null || t2 == null){
            return false;
        }
        return t1.val == t2.val
            &&isMirror(t1.left,t2.right)
            &&isMirror(t1.right,t2.left);
    }
}

(迭代方法)

借助队列遍历

public boolean isSymmetric(TreeNode root) {
         Queue<TreeNode> queue = new LinkedList<>();
           queue.add(root);
           queue.add(root);
         while(!queue.isEmpty()){
            TreeNode  t1 = queue.poll();
            TreeNode  t2 = queue.poll();
             if(t1 == null && t2 == null){
                 continue;
             }
             if(t1 == null || t2 == null){
                 return false;
             }
             if(t1.val != t2.val){
                 return false;
             }
              queue.add(t1.left);
              queue.add(t2.right);
              queue.add(t1.right);
              queue.add(t2.left);
         } 
         return true;
     }

 


层序遍历二叉树

一层一层的进行访问树中的元素

借助队列来实现非递归的二叉树遍历

给定一个二叉树,返回其按层次遍历的节点值。 (即逐层地,从左到右访问所有节点)。

例如:
给定二叉树: [3,9,20,null,null,15,7],

    3
   / \
  9  20
    /  \
   15   7


返回其层次遍历结果:

[
  [3],
  [9,20],
  [15,7]
]

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


解题:

Q1:如何将队列中的元素按层序遍历的顺序取出

先序递归遍历

Q2:如何按照输出结果的形式将层序遍历的结果输出

创建一个列表

class Solution {
     List<List<Integer>> levels = new ArrayList<List<Integer>>();

     public List<List<Integer>> levelOrder(TreeNode root) {
        if(root == null){
            return levels;
        }
        help(root , 0);
        return levels;
     }     
    public void help(TreeNode node ,int level){
        if(level == levels.size()){
            levels.add(new ArrayList<Integer>());
        }
        levels.get(level).add(node.val);
        if(node.left != null){
            help(node.left,level + 1);
        }
        if(node.right != null){
            help(node.right,level + 1);
        }
     } 
}

判断完全二叉树

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值