Leetcode #111 Minimum Depth of Binary Tree
2015-08-11 18:09
323 查看
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.
Difficulty:Easy
The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.
Difficulty:Easy
/** * 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 cal(int dep,TreeNode* root){ if(root==NULL) return 2147483647; if(root->left==NULL&&root->right==NULL) return 1+dep; return min(cal(dep+1,root->left),cal(dep+1,root->right)); } int minDepth(TreeNode* root) { if(root==NULL) return 0; if(root->left==NULL&&root->right==NULL) return 1; int ans = 2147483647; ans = min(ans,cal(1,root->left)); ans = min(ans,cal(1,root->right)); return ans; } };
相关文章推荐
- SecureCRT中文显示乱码
- C++对象模型——指向Member Function的指针 (Pointer-to-Member Functions)(第四章)
- runtime 运行时机制 完全解读
- 1.ios
- BluetoothGatt API
- 【黑马程序员】第一篇_关于String的认识
- Cookbook
- Go开发工具
- Cool Android Apis 整理(二)
- Android M 六大新特性
- 指向字符串常量的指针变量
- 使用MATLAB BNT贝叶斯工具箱,进行Gibbs Sampling之前需要做的编译工作
- C#基础知识
- hdu 5379 Mahjong tree dfs
- 【读书笔记】iOS-开发技巧-三种收起键盘的方法
- Cool Android Apis 整理(一)
- 计算从A地出发到各个地方的路径及距离
- 关于mac book 的启动选项
- kernel_task high cpu usage
- Android解惑 - 为什么要用Fragment.setArguments(Bundle bundle)来传递参数