LeetCode 111. Minimum Depth of Binary Tree
2016-07-22 13:44
489 查看
111. Minimum Depth of Binary Tree
My Submissions QuestionEditorial Solution
Total Accepted: 101139 Total Submissions: 331361 Difficulty: Easy
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.
My Submissions QuestionEditorial Solution
Total Accepted: 101139 Total Submissions: 331361 Difficulty: Easy
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 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 == NULL) return 0; if(root->left == NULL && root->right == NULL) return 1; if(root->left == NULL && root->right != NULL) return minDepth(root->right) + 1; if(root->right == NULL && root->left != NULL) return minDepth(root->left) + 1; if(root->left != NULL && root->right != NULL) { int leftdepth = minDepth(root->left); int rightdepth = minDepth(root->right); return leftdepth < rightdepth ? leftdepth + 1 : rightdepth + 1; } } };
相关文章推荐
- Java 中 Collection 与 Collections 的区别
- 杭电1502J - Tian Ji -- The Horse Racing
- LeetCode 237. Delete Node in a Linked List
- MVC 使用Response.Redirect页面301重定向
- [记忆化搜索]vijos 1547 逆转,然后再见
- vjP1955货币(记忆化搜索)
- FZU Problem 1894 志愿者选拔 单调队列
- 通过gradle批量打包apk时更改相关icon和label
- mysql数据恢复
- mac下创建.bash_profile步骤
- CC2530ZDK-ZNP-MINI
- Summer day 11
- LeetCode 100. Same Tree
- python-os.walk()使用举例
- ASP.NET CORE 1.0 MVC API 文档用 SWASHBUCKLE SWAGGER实现
- tuple 元组
- 新的博客:http://blog.csdn.net/bat67
- 26个提升java性能需要注意的地方
- linux下网络程序遭遇SIGPIPE信号进程退出的原因及规避方法
- Canvas 学习之 (2) translate