Leetcode 完全二叉树的节点个数 二(N)叉树的最大深度 最小深度

完全二叉树的节点个数

完全二叉树只有两种情况,情况一:就是满二叉树,情况二:最后一层叶子节点没有满

对于情况一,可以直接用 2^树深度 - 1 来计算,注意这里根节点深度为1

对于情况二,分别递归左孩子,和右孩子,递归到某一深度一定会有左孩子或者右孩子为满二叉树,然后依然可以按照情况1来计算。

本题我使用递归法解决

完整代码如下:

class Solution {
    public int countNodes(TreeNode root) {
        if(root==null){return 0;}
        int leftdepth=getDepth(root.left);
        int rightdepth=getDepth(root.right);
        if(leftdepth==rightdepth){ //左子树是满二叉树
            return (1<<leftdepth)+countNodes(root.right);
        }else{                     //右子树是满二叉树
            return (1<<rightdepth)+countNodes(root.left);
        }

    }
    private int getDepth(TreeNode node){
        int deep=0;
        while(node!=null){
            node =node.left;
            deep++;
        }
        return deep;
    }
}

注意:1<<leftdepth 表示 leftdepth^2次方(实际上是leftdepth^2-1+1,加上根节点)

           递归时每次返回此时的满二叉树节点数+下一子树遍历的节点数

           遍历到最后一层时当无右子树节点时(即左右子树节点不一致),则总结点数为当前节点数的2次方+其子树节点遍历的节点数。

二(N)叉树的最大深度

根节点的高度就是二叉树的最大深度。

可分为迭代法和递归法。

迭代法:使用层次遍历,只需要多定义一个深度 每次遍历下一层深度就加一,最后返回深度。

递归法:可使用前序遍历,也可以使用后序遍历。

               前序遍历求深度,后序遍历求高度

递归步骤:1.确定递归函数的参数和返回值。

                    参数为传入的根节点 返回值为深度(int类型)

                  2.确定终止条件。

                    空节点返回0,表示高度为0

                  3.确定单层递归的逻辑。

                   先求它的左子树的深度,再求右子树的深度,最后取左右深度最大的数值再+1(算上                     当前中间节点)就是目前节点为根节点的树的深度。

                   注意:返回两者最大值 Math.max

    另一题N叉树的最大深度也类似。

N叉树代码区别:

迭代法

递归法

 

二叉树完整代码如下:

 迭代法

class Solution {
    public int maxDepth(TreeNode root) {
        if(root==null){return 0;}
        Queue<TreeNode> queue = new LinkedList<TreeNode>();
        queue.offer(root);
        int depth=0;
        while(!queue.isEmpty()){
            int size=queue.size();
            depth++;
            while(size-->0){ //前序遍历求深度
                TreeNode node1=queue.poll();
                if(node1.left!=null){queue.offer(node1.left);}
                if(node1.right!=null){queue.offer(node1.right);}
            }
        }
        return depth;
    }
}

递归法

class Solution {
    public int maxDepth(TreeNode root) {
    if(root==null){return 0;}
    int leftdepth =maxDepth(root.left);
    int rightdepth =maxDepth(root.right);
    return Math.max(leftdepth,rightdepth)+1;
    }
}

二叉树的最小深度

二叉树的最小深度:

最小深度是从根节点到最近叶子节点的最短路径上的节点数量。    注意是叶子节点

什么是叶子节点?

左右孩子都为空的节点才是叶子节点。

也可分为递归法和迭代法

递归法:

求二叉树的最小深度和求二叉树的最大深度的差别主要在于处理左右孩子不为空的逻辑

如果左子树为空,右子树不为空,说明最小深度是 1 + 右子树的深度

反之,右子树为空,左子树不为空,最小深度是 1 + 左子树的深度

最后如果左右子树都不为空,返回左右子树深度最小值 + 1

迭代法:使用层次遍历

需要注意的是,只有当左右孩子都为空的时候,才说明遍历的最低点了。如果其中一个孩子为空则不是最低点

完整代码如下:

递归法:

class Solution {
    public int minDepth(TreeNode root) {
        if(root==null){return 0;}
        int leftdepth =minDepth(root.left);
        int rightdepth =minDepth(root.right);
        if(root.left==null){
            return 1+rightdepth;
        }
        if(root.right==null){
            return 1+leftdepth;
        }
        return Math.min(leftdepth,rightdepth)+1;
    }
}

迭代法:

class Solution {
    public int minDepth(TreeNode root) {
        if(root==null){return 0;}
        Queue<TreeNode> queue =new LinkedList<>();
        int depth=0;
        queue.offer(root);
        while(!queue.isEmpty()){
            int size =queue.size();
            depth++;
            while(size-->0){
                TreeNode node =queue.poll();
                if(node.left==null&&node.right==null){
                    return depth;
                }
                if(node.left!=null){queue.offer(node.left);}
                if(node.right!=null){queue.offer(node.right);}
            }
        }
        return depth;
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值