星空网 > 软件开发 > Java

111. Minimum Depth of Binary Tree

 

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 public class Solution {11   12   public int minDepth(TreeNode root) {13     14     Stack<Integer> stack=new Stack<>();15     int depth=0;16    if(root==null)17    return 0;18    19    if(root.left==null&&root.right==null)20    {return 1;}21    22    if(root.left!=null)23     {24      depth=1+minDepth(root.left);25 26      if(stack.empty()||stack.peek()>depth)27       stack.push(depth);28     }29    30    31     if(root.right!=null)32     {33      depth=1+minDepth(root.right);34       if(stack.empty()||stack.peek()>depth)35       stack.push(depth);36     }37    38    return stack.peek();39   }40 }

 




原标题:111. Minimum Depth of Binary Tree

关键词:

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

tiktok变现流程:https://www.goluckyvip.com/tag/83917.html
tiktok第三方:https://www.goluckyvip.com/tag/83918.html
tiktok的梯子:https://www.goluckyvip.com/tag/83919.html
Twitter财报:https://www.goluckyvip.com/tag/8392.html
tiktok的算法:https://www.goluckyvip.com/tag/83920.html
tiktok小店英国:https://www.goluckyvip.com/tag/83921.html
七月份适合去日本旅游吗 7月份去日本哪里好玩:https://www.vstour.cn/a/363192.html
凤岗汽车站到松山湖风景区 松岗汽车站到凤凰山怎么坐车:https://www.vstour.cn/a/363193.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流