博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Minimum Depth of Binary Tree
阅读量:4692 次
发布时间:2019-06-09

本文共 688 字,大约阅读时间需要 2 分钟。

Given a binary tree, find its minimum depth.

The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.

 

1 /** 2  * Definition for a binary tree node. 3  * public class TreeNode { 4  *     int val; 5  *     TreeNode left; 6  *     TreeNode right; 7  *     TreeNode(int x) { val = x; } 8  * } 9  */10 class Solution {11     public int minDepth(TreeNode root) {12         if(root == null) return 0;13         int left = minDepth(root.left);14         int right = minDepth(root.right);15         return (left == 0 || right == 0) ? left + right + 1: Math.min(left,right) + 1;16     }17 }

 

转载于:https://www.cnblogs.com/GodBug/p/7392627.html

你可能感兴趣的文章
打卡-反射基础(二)
查看>>
5-18
查看>>
redis缓存数据库
查看>>
<ul>下<li>的list-style属性
查看>>
hello world2
查看>>
在子窗口中操作父窗口(刷新)
查看>>
maven insall跳过测试
查看>>
B树 B- B+ B*
查看>>
『算法设计_伪代码』红黑树
查看>>
CentOS 配置RDP
查看>>
简单的触发黑名单阻断演示 control+c
查看>>
Adobe出品(支持IOS,android,web调用)免费插件编辑图片
查看>>
如何恢复windows的exe文件的默认打开方式
查看>>
codewars--js--Convert all the cases!
查看>>
codeforce440C-Maximum splitting-规律题
查看>>
牛客小白月赛8 - E - 诡异数字 数位DP
查看>>
@Autowired还可以注入List和Map
查看>>
004 使用文本编辑器
查看>>
RAID5当一块硬盘离线后处理
查看>>
我的系统备份策略
查看>>