代码随想录day16

代码随想录day16

104.二叉树的最大深度

迭代(层序遍历)

class Solution {
    public int maxDepth(TreeNode root) {
      List<TreeNode> pre = new ArrayList<>();
        pre.add(root);
        if(root == null){
            return 0;
        }
        int num = 0;
        while(!pre.isEmpty()){
            List<TreeNode> tmp = pre;
            pre = new ArrayList<>();
            for(int i=0 ; i<tmp.size() ; i++){
                TreeNode node = tmp.get(i);
                if(node.left != null){
                    pre.add(node.left);
                }
                if(node.right != null){
                    pre.add(node.right);
                }
            }
            num++;
        }
        return num;
    }
}    

递归

class Solution {
    int num = 0;
    public int maxDepth(TreeNode root) {
        ans(root,0);
        return num;


    }

    void ans(TreeNode tr , int tmp){
        if(tr == null){
            return;
        }

        tmp++;
        num = Math.max(num,tmp);
        ans(tr.left,tmp);
        ans(tr.right,tmp);
        tmp--;
    }
}

559.N叉树的最大深度

迭代

class solution {
   public int maxDepth(Node root) {
        if (root == null)   return 0;
        int depth = 0;
        Queue<Node> que = new LinkedList<>();
        que.offer(root);
        while (!que.isEmpty())
        {
            depth ++;
            int len = que.size();
            while (len > 0)
            {
                Node node = que.poll();
                for (int i = 0; i < node.children.size(); i++)
                    if (node.children.get(i) != null) 
                        que.offer(node.children.get(i));
                len--;
            }
        }
        return depth;
    }
}

递归

class Solution {  
    public int maxDepth(Node root) {  
        if (root == null) {  
            return 0; // 如果根节点为空,则深度为0  
        }  
        int maxChildDepth = 0;  
        // 遍历当前节点的所有子节点  
        for (Node child : root.children) {  
            // 递归计算每个子节点的最大深度  
            int childDepth = maxDepth(child);  
            // 更新最大子树深度  
            maxChildDepth = Math.max(maxChildDepth, childDepth);  
        }  
        // 返回当前节点的深度,即最大子树深度加1(当前层的深度)  
        return maxChildDepth + 1;  
    }  
}

111.二叉树的最小深度

迭代

class Solution {
    public int minDepth(TreeNode root) {
       List<TreeNode> pre = new ArrayList<>();
        pre.add(root);
        if(root == null){
            return 0;
        }
        int num = 0;
        while(!pre.isEmpty()){
            num++;
            List<TreeNode> tmp = pre;
            pre = new ArrayList<>();
            for(int i=0 ; i<tmp.size() ; i++){
                TreeNode node = tmp.get(i);
                if(node.left == null && node.right == null){
                    return num;
                }
                if(node.left != null){
                    pre.add(node.left);
                }
                if(node.right != null){
                    pre.add(node.right);
                }
            }
            
        }
        return num;
    }
}    

递归

class Solution {
    public int minDepth(TreeNode root) {
        if(root == null){
            return 0;
        }

        int left = minDepth(root.left);
        int right = minDepth(root.right);

        if(root.left == null){
            return right+1;
        }
        if(root.right == null){
            return left+1;
        }

        return Math.min(left,right)+1;

    }
}

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

class Solution {
    public int countNodes(TreeNode root) {
        if (root == null) return 0;
        LinkedList<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        int result = 0;
        while (!queue.isEmpty()) {
            int size = queue.size();
            while (size -- > 0) {
                TreeNode cur = queue.poll();
                result++;
                if (cur.left != null) queue.offer(cur.left);
                if (cur.right != null) queue.offer(cur.right);
            }
        }
        return result;
    }
}    
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值