day15 | 104.二叉树的最大深度,111.二叉树的最小深度,222.完全二叉树的节点个数

104.二叉树的最大深度

深度:任意一节点到根节点的距离
高度:任意一节点到叶子节点的距离
求高度:后序遍历(左右中),从下往上去计数,叶子节点将结果返回给父节点
父节点+1,就是父节点的高度。根节点的高度就是二叉树的最大深度。
求深度:前序遍历(中左右),往下遍历一个就+1
在这里插入图片描述

求根节点的高度:根节点的高度就是二叉树的深度
后序遍历:
左右中
int getHeight( node) {
if (node == null) return 0; // 高度为0
左:int leftHeight = getHeight(node.left);
右:int rightHeight = getHeight(node.right);
中:int height = 1 + max(leftHeight, rightHeight); //当前节点的父节点的高度
return height;
}
精简版:
return 1 + max(getHeight(node.left, getHeight(node.right));

题解

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;
    }
}

559.n叉树的最大深度

class Solution {
    /*递归法,后序遍历求root节点的高度*/
    public int maxDepth(Node root) {
        if (root == null) return 0;

        int depth = 0;
        if (root.children != null){
            for (Node child : root.children){
                depth = Math.max(depth, maxDepth(child));
            }
        }

        return depth + 1; //中节点
    }  
}

111.二叉树的最小深度

最小深度:根节点到最近的叶子节点的最小距离
如下图,最小深度为3
在这里插入图片描述
求根节点的最小高度得最小深度
int getHeight( TreeNode node) {
if(node == null) return 0;
左:int leftHeight = getHeight(node.left);
右:int rightHeight = getHeight(node.right);
if(node.left == null && node.right != null) return 1 + rightHeight;
if(node.left != null && node.right == null) return 1 + leftHeight;
//左右子树都不为空:
result = 1 + min(leftHeight, rightHeight);
return result;

题解

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 && root.right != null) return 1 + rightDepth;
        if (root.left != null && root.right == null) return 1 + leftDepth;

        return Math.min(leftDepth, rightDepth) + 1;
    }
}

222.完全二叉树的节点个数

在这里插入图片描述
后序遍历:
普通二叉树:
int getNum(node) {
if(node == null) return 0;
leftNum = getNum(node.left);
rightNum = getNum(node.right);
result = leftNum + rightNum + 1;
return result;
精简:
return getNum(node.left) + getNum(node.right) + 1;
完全二叉树:
可以先判断是不是满二叉树,如果左右子树的深度相同,则是满二叉树,可以用
公式2的n次方-1,单个叶子节点一定是满二叉树


int getNum(node) {
if(node == null) return 0;
left = node.left;
right = node.right;
leftDepth = 0; //左侧深度
rightDepth = 0; //右侧深度
while (left) {
left = left.left;
leftDepth++;
}
while (right) {
right = right.right;
rightDepth ++;
}
if (leftDepth == rightDepth) return 2^leftDepth - 1;
leftNum = getNum(node.left);
rightNum = getNum(node.right);
result = leftNum + rightNum + 1;
return result;

题解

class Solution {
    // 通用递归解法
    public int countNodes(TreeNode root) {
        if(root == null) {
            return 0;
        }
        return countNodes(root.left) + countNodes(root.right) + 1;
    }
}
class Solution {
    /**
     * 针对完全二叉树的解法
     *
     * 满二叉树的结点数为:2^depth - 1
     */
    public int countNodes(TreeNode root) {
        if (root == null) return 0;
        TreeNode left = root.left;
        TreeNode right = root.right;
        int leftDepth = 0, rightDepth = 0; // 这里初始为0是有目的的,为了下面求指数方便
        while (left != null) {  // 求左子树深度
            left = left.left;
            leftDepth++;
        }
        while (right != null) { // 求右子树深度
            right = right.right;
            rightDepth++;
        }
        if (leftDepth == rightDepth) {
            return (2 << leftDepth) - 1; // 注意(2<<1) 相当于2^2,所以leftDepth初始为0
        }
        return countNodes(root.left) + countNodes(root.right) + 1;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值