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

本文介绍了在代码随想录算法训练营中涉及的四个问题:二叉树的最大深度计算、n叉树的最大深度、二叉树的最小深度以及完全二叉树的节点个数。通过递归和迭代方法探讨了这些数据结构的深度计算策略。
摘要由CSDN通过智能技术生成

代码随想录算法训练营

今日任务
104.二叉树的最大深度 ,559.n叉树的最大深度 ,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 {
    public int maxDepth(TreeNode root) {
        //递归,利用后序排序
        return getDepth(root);
    }
    public int getDepth(TreeNode root){
        if(root==null){
            //叶子节点
            //其实深度就是从叶子节点开始向上加的
            return 0;
        }
        int leftDepth = getDepth(root.left);
        //System.out.println(leftDepth);
        int rightDepth = getDepth(root.right);
        //System.out.println(rightDepth);
        return 1+Math.max(leftDepth,rightDepth);
    }
}

559.n叉树的最大深度

/*
// Definition for a Node.
class Node {
    public int val;
    public List<Node> children;

    public Node() {}

    public Node(int _val) {
        val = _val;
    }

    public Node(int _val, List<Node> _children) {
        val = _val;
        children = _children;
    }
};
*/

class Solution {
    public int maxDepth(Node root) {
        //迭代法
        //利用队列
        if(root==null){
            return 0;
        }
        if(root.children==null){
            return 1;
        }
        Queue<Node> queue=new LinkedList<>();
        queue.offer(root);
        int depth=0;
        while (!queue.isEmpty()){
            depth++;
            int size=queue.size();
            while(size>0){
                size--;
                Node top = queue.poll();
                List<Node> children = top.children;
                if(children==null){
                    continue;
                }
                for (Node child : children) {
                    queue.offer(child);
                }
            }
        }
        return depth;
    }
    
}

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 {
    public int minDepth(TreeNode root) {
        int result=0;
        if(root==null){
            return result;
        }
        Queue<TreeNode> queue=new LinkedList<>();
        queue.offer(root);
        result++;
        int flag=0;
        while(!queue.isEmpty()){
            int size=queue.size();
            while(size>0){
                size--;
                TreeNode poll = queue.poll();
                if(poll.left!=null){
                    queue.offer(poll.left);
                }
                if(poll.right!=null){
                    queue.offer(poll.right);
                }
                if(poll.right==null&&poll.left==null){
                    //叶子节点
                    flag=1;
                    break;
                }
            }
            if(flag==0){
                result++;
            }else{
                break;
            }
        }
        return result;
    }
}

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

/**
 * 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 {
    public int countNodes(TreeNode root) {
        return getDepth(root);
    }
    public int getDepth(TreeNode node) {
        if(node==null){
            return 0;
        }
        int leftDepth=getDepth(node.left);
        int rightDepth=getDepth(node.right);
        //String str=node.val+" "+leftDepth+" "+rightDepth;
        //System.out.println(str);
        return leftDepth+rightDepth+1;
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值