Leetcode[111]-Minimum Depth of Binary Tree
2015-06-11 22:23
274 查看
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.
递归法:
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 && !root->right) return 1; int dleft=0,dright=0,dmin = 0; if(root->left) dleft = minDepth(root->left) + 1 ; if(root->right) dright = minDepth(root->right) + 1; if(!root->left && root->right) return dright; if(!root->right && root->left) return dleft; dmin = dleft>dright?dright:dleft; return dmin; } };
相关文章推荐
- 端口映射帮助文档
- org.hibernate.MappingException: No Dialect mapping for JDBC type: -4
- 感知机、线性回归、逻辑回归的简单对比
- leetcode 018 4sum (Python)
- org.hibernate.MappingException: No Dialect mapping for JDBC type: -4
- org.hibernate.MappingException: No Dialect mapping for JDBC type: -4
- Web前端或成互联网+时代的职场新贵
- C#使用资源字典
- 第1章 Java程序设计概述
- ARC的内存管理
- WordClassification API
- 什么是道德?
- 猜数字小游戏的总结
- Python高级编程之生成器(Generator)与coroutine(四):一个简单的多任务系统
- Pronunciation API
- 文章标题
- FastDictionary API
- Swift - 枚举
- Dictionary API
- 算法练习:最大下标距离