111. Minimum Depth of Binary Tree
来源:互联网 发布:淘宝网上批发市场 编辑:程序博客网 时间:2024/06/16 17:20
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.
Subscribe to see which companies asked this question.
Solution:
Tips:
post order visit
Java Code:
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { // post order visit public int minDepth(TreeNode root) { if (null == root) { return 0; } int leftDepth = minDepth(root.left); int rightDepth = minDepth(root.right); return leftDepth == 0 || rightDepth == 0 ? leftDepth + rightDepth + 1 : Math.min(leftDepth, rightDepth) + 1; }}
0 0
- LeetCode 111. Minimum Depth of Binary Tree
- [LeetCode]111.Minimum Depth of Binary Tree
- 【LeetCode】111.Minimum Depth of Binary Tree
- [Leetcode] 111. Minimum Depth of Binary Tree
- 111.Minimum Depth of Binary Tree
- 111. Minimum Depth of Binary Tree
- [leetcode] 111. Minimum Depth of Binary Tree
- 111. Minimum Depth of Binary Tree
- 111. Minimum Depth of Binary Tree
- 111. Minimum Depth of Binary Tree
- 111. Minimum Depth of Binary Tree LeetCode
- 111. Minimum Depth of Binary Tree
- 111. Minimum Depth of Binary Tree
- 【LeetCode】111. Minimum Depth of Binary Tree
- 111. Minimum Depth of Binary Tree
- 111. Minimum Depth of Binary Tree
- LeetCode *** 111. Minimum Depth of Binary Tree
- 111. Minimum Depth of Binary Tree
- 【python学习笔记】1- 相关环境安装
- MySQL数据库(入门)(一)
- shell中变量的变量与eval命令
- 团体程序设计天梯赛L3-013 非常弹的球
- 【密码学】SHA-1加密原理及其Java实现
- 111. Minimum Depth of Binary Tree
- 四、SpringBoot属性配置文件详解
- 洛谷 P2915 [USACO08NOV]奶牛混合起来Mixed Up Cows
- 【python学习笔记】1:环境安装
- 团体程序设计天梯赛L3-014 周游世界
- poj 1321棋盘问题
- 【C语言】*++p,*p++,++*p,++*++p,++*p++
- 团体程序设计天梯赛L2-018 多项式A除以B
- 2017年2月中华人民共和国县以上行政区划代码