leetcode之Minimum Depth of Binary Tree
2016-04-11 21:36
295 查看
采用递归思想,这次是求最小深度。
(1)C语言实现
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
int min(int x, int y){
return x<y?x:y;
}
int minDepth(struct TreeNode* root) {
if(!root)
return 0;
if(!root->left)
return 1+minDepth(root->right);
if(root->right==NULL)
return 1+minDepth(root->left);
return 1+min(minDepth(root->left), minDepth(root->right));
}
(2)C++实现
/**
* Definition for a binary tree node.
* 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)
return 0;
if(!root->left)
return 1+minDepth(root->right);
if(!root->right)
return 1+minDepth(root->left);
return min(minDepth(root->left), minDepth(root->right))+1;
}
};
(3)java实现
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public int minDepth(TreeNode root) {
if(root == null)
return 0;
if(root.left == null)
return 1+minDepth(root.right);
if(root.right == null)
return 1+minDepth(root.left);
return 1+Math.min(minDepth(root.left), minDepth(root.right));
}
}
(1)C语言实现
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
int min(int x, int y){
return x<y?x:y;
}
int minDepth(struct TreeNode* root) {
if(!root)
return 0;
if(!root->left)
return 1+minDepth(root->right);
if(root->right==NULL)
return 1+minDepth(root->left);
return 1+min(minDepth(root->left), minDepth(root->right));
}
(2)C++实现
/**
* Definition for a binary tree node.
* 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)
return 0;
if(!root->left)
return 1+minDepth(root->right);
if(!root->right)
return 1+minDepth(root->left);
return min(minDepth(root->left), minDepth(root->right))+1;
}
};
(3)java实现
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public int minDepth(TreeNode root) {
if(root == null)
return 0;
if(root.left == null)
return 1+minDepth(root.right);
if(root.right == null)
return 1+minDepth(root.left);
return 1+Math.min(minDepth(root.left), minDepth(root.right));
}
}
相关文章推荐
- java.util.Collections.unmodifiableMap()方法实例
- 第六周项目4 静态成员应用
- [范例] Firemonkey TForm 实现 OnMouseLeave 事件 (適用 Win & OS X)
- 浅谈C++容器(二)
- 字符串反转函数
- 自定义ImageView加载圆形图片
- 浅谈C++容器(一)
- WebService学习整理-JDK的wsimport命令和cxf的wsdl2java命令的区别和使用
- linux平台下虚拟内存管理
- [Lintcode]Space Replacement
- poj 2251 Dungeon Master
- SQL查询语句总结
- webservice 入门笔记四发送header信息
- 802.11协议精读3:CSMA/CD与CSMA/CA
- Java对IpAddress排序
- [LeetCode] Combination Sum III
- JS实现div块的拖放,调换位置
- 无限级分类新闻管理数据表设计
- cv1.3计算物体旋转角
- hdoj 5651 xiaoxin juju needs help (排列组合)