首页 > 其他分享 >[代码随想录] 第十四天

[代码随想录] 第十四天

时间:2024-01-25 17:56:14浏览次数:26  
标签:right TreeNode int 代码 随想录 第十四天 return root left

222.完全二叉树的节点个数[https://leetcode.cn/problems/count-complete-tree-nodes/submissions/498293461/]
思路:递归法

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

思路:迭代法

/**
 * 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) {
        int x = 0;
        if (root == null) {
            return 0;
        }
        Queue<TreeNode> que = new LinkedList<>();
        que.offer(root);
        while (!que.isEmpty()) {
            List<Integer> list = new ArrayList<>();
            int loop = que.size();
            for (int i = 0; i < loop; i++) {
                TreeNode temp = que.poll();

                if (temp.left != null) {
                    que.offer(temp.left);
                }
                if (temp.right != null) {
                    que.offer(temp.right);
                }
                x++;
            }
        }
        return x;
    }
}
**-----------------------分割线-------------------------**

104.二叉树的最大深度[https://leetcode.cn/problems/maximum-depth-of-binary-tree/submissions/498058606/]
思路:递归法。迭代法在第十三天的博客中。

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;
    }
}
**-----------------------分割线-------------------------**

111.二叉树的最小深度[https://leetcode.cn/problems/minimum-depth-of-binary-tree/]
思路:递归法。迭代法在第十三天的博客中。

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

标签:right,TreeNode,int,代码,随想录,第十四天,return,root,left
From: https://www.cnblogs.com/cssg/p/17987799

相关文章

  • python代码
    #读取.mat文件mat_data=scipy.io.loadmat('C.mat')#获取矩阵数据adj_matrix=mat_data['C']一个简短的python代码:alph=['A','B','C','D','E','F','G','H','I','J......
  • Python基础语法:代码规范、判断语句与循环语句
    Python是一种高级、动态类型的编程语言,其语法清晰、简洁,易于学习。本文将介绍Python基础语法中的代码规范、判断语句和循环语句。一、代码规范良好的代码规范可以提高代码的可读性和可维护性。在Python中,有一些常见的代码规范建议:使用有意义的变量名。变量名应该清晰地描述变量的用......
  • 源码文件阅读---hooks的使用---使用文心一言读代码
    import{CommonFormTypes}from'globalConstants';import{createContext,useCallback,useState}from'react';import{BoardType}from'../../../DashBoardPage/pages/Board/slice/types';import{VizType}from'./slic......
  • 18、拷贝构造、赋值构造、移动构造的简洁代码实现
    classBuffer{public:explicitBuffer(intcapacity):capacity_(capacity),len_(0),buff_(newchar[capacity]{0}){std::cout<<"默认的构造函数"<<std::endl;};~Buffer(){};Buffer(constBuffer&other)n......
  • 计网笔记:python实现简单的UDP/TCP代码
    初学计网,同时也是第一次写blog,若有不妥之处请多多包涵......
  • vo/dto/entity关于代码规范
    简明扼要的说明一下,vo/dto/entity分别是什么,什么时候该用谁。vo:是用来把数据返回前端的载具为什么:因为普遍来说,会在vo里写上有关分页的属性(page/size等);除此之外,vo里放的属性,应该与你的业务逻辑没有半毛钱关系,单纯就是前端要啥你写啥。entity:就是体现你数据库的表结构,你表有......
  • 用python实现部分代码内容替换的功能(用关键字实现)
    具体实现方法先将exl表格中的数据提取出来存放到list中,再根据文件中对应部分的需求和结构来构造函数去生成相应部分的c语言代码,然后通过readlines函数扫描原本的文件,按行复制到新的文件中,在识别到开始关键字的时候停止复制,开始调用构造的函数去生成新的代码写入新的文件中,然后在......
  • 代码模板
    代码模板数论快速幂intqmi(inta,intb,intp){ intres=1; while(b){ if(b&1)res=res*a%p; a=a*a%p; b>>=1; } returnres;}线性筛法(素数+欧拉函数)intst[N1],pri[N1],cnt,phi[N1];intgetp(intn){ phi[1]=1; for(inti=......
  • 10个问题,教你如何使用一个事半功倍的代码托管平台
    源代码是企业最宝贵的资产之一,随着软件规模的不断扩大,企业管理庞大的源代码成为一个重大挑战。为了保持企业员工持续稳定地开展软件开发活动,一个好用的代码管理平台变得尤为重要。代码管理工具是软件开发的基础,能够帮助团队协作更加高效,自动化交付更加流畅。因此,选择一款优秀的代码......
  • 快捷方式,我的代码片段库
    服务端功能接收上传图片,并保存到本地varfiles=Request.Form.Files;varfile=files[0];varfileSavePath="c:\upload\filename.img";using(FileStreamfs=System.IO.File.Create(fileSavePath)){file.CopyTo(fs);//保存原图fs.Flush();}.net类库字符串......