当前位置:网站首页>Leetcode:minimum_depth_of_binary_tree解决问题的方法
Leetcode:minimum_depth_of_binary_tree解决问题的方法
2022-07-06 18:47:00 【全栈程序员站长】
大家好,又见面了,我是全栈君
一、 称号
并寻求最深的二元相似。给定的二进制树。求其最小深度。
最小深度是沿从根节点,到叶节点最短的路径。
二、 分析
当我看到这个题目时。我直接将最深二叉树的代码略微改了下,把max改成min。本以为应该没有问题,谁知道WA了两次,我静下来看了看。最终知道了,当遇到有结点为NULL时就得要结束了。所下面次再简单的题目也要静下来好好分析,不然会easy出错。
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int minDepth(TreeNode *root) {
if(root==NULL)
return 0;
int mleft=minDepth(root->left);
int mright=minDepth(root->right);
if(mleft==0)
return 1+mright;
else if(mright==0)
return 1+mleft;
else return min(mleft,mright)+1;
}
};
二、
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int minDepth(TreeNode *root) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
return minRec(root);
}
int minRec( TreeNode * root) {
if(!root) return 0;
int left = minRec( root->left);
int right = minRec( root->right);
if(left && right) return 1 + min(left, right);
if(left || right) return 1+left+right;
return 1;
}
};
三、
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public int minDepth(TreeNode root) {
// Start typing your Java solution below
// DO NOT write main() function
return minRec(root);
}
private int minRec(TreeNode root) {
if(root==null) return 0;
int l = minRec(root.left);
int r = minRec(root.right);
if(l==0) return r+1;
if(r==0) return l+1;
return Math.min(l, r) + 1;
}
}版权声明:本文博主原创文章,博客,未经同意不得转载。
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/116848.html原文链接:https://javaforall.cn
边栏推荐
- Pioneer of Web3: virtual human
- postgresql 之 数据目录内部结构 简介
- leetcode:736. LISP syntax parsing [flowery + stack + status enumaotu + slots]
- The empirical asset pricing package (EAP) can be installed through pypi
- Ali yunyili: how does yunyuansheng solve the problem of reducing costs and improving efficiency?
- Blackfly s usb3 industrial camera: buffer processing
- unity中跟随鼠标浮动的面板,并可以自适应文字内容的大小
- Time synchronization of livox lidar hardware -- PPS method
- 长安链学习笔记-证书研究之证书模式
- 机器人队伍学习方法,实现8.8倍的人力回报
猜你喜欢
![[paper reading | deep reading] rolne: improving the quality of network embedding with structural role proximity](/img/21/59c4d61b22e7d021b7a72a64a16b48.png)
[paper reading | deep reading] rolne: improving the quality of network embedding with structural role proximity

张平安:加快云上数字创新,共建产业智慧生态
![leetcode:736. LISP syntax parsing [flowery + stack + status enumaotu + slots]](/img/0d/e07fe970167368040eb09b05c3682e.png)
leetcode:736. LISP syntax parsing [flowery + stack + status enumaotu + slots]

Compress JS code with terser

leetcode:736. Lisp 语法解析【花里胡哨 + 栈 + 状态enumaotu + slots】

如何从0到1构建32Core树莓派集群

将截断字符串或二进制数据

Lumion 11.0软件安装包下载及安装教程
![[paper reading | deep reading] dngr:deep neural networks for learning graph representations](/img/8e/41a80131c6ec7a7cdffe4a77012fed.png)
[paper reading | deep reading] dngr:deep neural networks for learning graph representations

阿里云易立:云原生如何破解企业降本提效难题?
随机推荐
Draco - glTF模型压缩利器
Recent applet development records
[paper reading | deep reading] graphsage:inductive representation learning on large graphs
企业中台建设新路径——低代码平台
Draco - gltf model compression tool
Recommended collection!! Which is the best flutter status management plug-in? Please look at the ranking list of yard farmers on the island!
最近小程序开发记录
[paper reading | deep reading] rolne: improving the quality of network embedding with structural role proximity
张平安:加快云上数字创新,共建产业智慧生态
ODBC database connection of MFC windows programming [147] (with source code)
Web3的先锋兵:虚拟人
Compress JS code with terser
Lumion 11.0 software installation package download and installation tutorial
Data connection mode in low code platform (Part 1)
纽约大学 CITIES 研究中心招聘理学硕士和博士后
Pioneer of Web3: virtual human
4--新唐nuc980 挂载initramfs nfs文件系统
The mega version model of dall-e MINI has been released and is open for download
FLIR blackfly s usb3 industrial camera: white balance setting method
What to do when encountering slow SQL? (next)