代码随想录算法训练营第十六天|104.二叉树的最大深度、111.二叉树的最小深度、222.完全二叉树的节点个数

104.二叉树的最大深度

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    //DFS递归解法
    public int getdepth(TreeNode cur){
        if(cur == null){
            return 0;
        }
        //必须使用后序处理,处理完左右结点后,
        //带着左右孩子的信息返回给中间结点
        int leftdepth = getdepth(cur.left);
        int rightdepth = getdepth(cur.right);
        int depth = Math.max(leftdepth, rightdepth) + 1;
        return depth;
    }

    public int maxDepth(TreeNode root) {
        return getdepth(root);
    }
}

111.二叉树的最小深度

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    //DFS递归求最小深度
    public int getminDepth(TreeNode cur){
        if(cur == null){
            return 0;
        }
        int leftDepth = getminDepth(cur.left);
        int rightDepth = getminDepth(cur.right);
        //如果有一个子树为空,则最小高度取非空子树高度+1
        if(cur.left == null && cur.right != null){
            return rightDepth + 1;
        }
        if(cur.right == null && cur.left != null){
            return leftDepth + 1;
        }
        //如果两个子树均不为空
        int result = Math.min(leftDepth, rightDepth) + 1;
        return result;
    }
    //主函数
    public int minDepth(TreeNode root) {
        return getminDepth(root);
    }
}

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

        

2 << leftDepth
        该代码的意思是将数字2向左移动leftDepth位。
        在二进制中,向左移动一位相当于乘以2,向左移动n位相当于乘以2的n次方。
  • 题解(递归法DFS):
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    //递归DFS解法
    public int countNodes(TreeNode root) {
        if(root == null){
            return 0;
        }
        TreeNode curleft = root.left;
        TreeNode curright = root.right;
        int leftDepth = 0, rightDepth = 0;
        //求左子树左分支深度
        while(curleft != null){
            curleft = curleft.left;
            leftDepth++;
        }
        //求右子树右分支深度
        while(curright != null){
            curright = curright.right;
            rightDepth++;
        }
        //若做右分支深度相同,又因为二叉树为完全二叉树
        //所以此时当前二叉树一定为满二叉树
        //此时利用满二叉树结点总数公式进行计算
        if(leftDepth == rightDepth){
            return (2 << leftDepth) - 1;
        }
        //如果做右分支深度不同,则当前二叉树并不是满二叉树
        //则递归,继续向下遍历
        //因为最坏情况下会遍历到叶子结点,而叶子结点必然是满二叉树,左右分支深度均为0
        //所以向下递归遍历下去一定会得到满二叉树,然后计算当前结点总数并逐层返回
        return countNodes(root.left) + countNodes(root.right) + 1;
    }
}

  • 28
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值