Minimum Depth of Binary Tree
来源:互联网 发布:it项目管理案例 编辑:程序博客网 时间:2024/05/16 06:50
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.
/** * 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) { // Note: The Solution object is instantiated only once and is reused by each test case. if(root==null){ return 0; } else if(root.left==null&&root.right==null){ return 1; } else if(root.left==null&&root.right!=null){ return 1+minDepth(root.right); } else if(root.left!=null&&root.right==null){ return 1+minDepth(root.left); } else { return minDepth(root.left)>minDepth(root.right)?1+minDepth(root.right):1+minDepth(root.left); } } }
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Javascript 实现的数独解题算法网页实例
- OpenCV角点检测
- YCbCr与YUV的区别
- 数据库 ADO 另一种使用方法和关于C++引用的两个小点【2013.10.16 小记】
- Flash和EEPROM的区别
- Minimum Depth of Binary Tree
- LU分解版本3_选主元、进行本地存储
- cocosd-x教程4 CCTextFieldTTF的使用
- StrutsPrepareAndExecuteFilter的作用
- 生产者消费者问题理解与Java实现
- 转:InvokeHelper函数的用法
- 4399面试总结
- 宇龙酷派笔试题:求出数组的倒置,函数原型为char * reverse( char * str)
- 笔试题中常见的文件操作