leetcode[111]:Minimum Depth of Binary Tree
2015-06-15 19:35
211 查看
Minimum Depth of Binary Tree
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.
递归查询,注意一个子树为空的情况。
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; * struct TreeNode *left; * struct TreeNode *right; * }; */ int minDepth(struct TreeNode* root) { int i, j; i=j=1; if(!root) return 0; if(!root->left && !root->right) return 1; if(root->left && root->right) { i += minDepth(root->left); j += minDepth(root->right); if( i < j ) return i; return j; } if(!root->left && root->right) {i += minDepth(root->right); return i;} if(!root->right && root->left) {j += minDepth(root->left); return j;} }
递归查询,注意一个子树为空的情况。
相关文章推荐
- 命令行快速技巧:如何定位一个文件
- cmd tree命令 以树形格式罗列文件
- 推荐8款jQuery轻量级树形Tree插件
- tree 以树形格式罗列文件
- EasyUI Tree+Asp.net实现权限树或目录树导航的简单实例
- E3 tree 1.6在Firefox下显示问题的修复方法
- js树形控件脚本代码
- MYSQL的binary解决mysql数据大小写敏感问题的方法
- Spark Decision Tree
- 带check的treeView
- extjs 4 treepanel locked and expand
- JQuery EasyUI 实现tree的右键菜单
- flex tree 的两个小技巧
- Tree.log
- gnome文件管理器(nautilus)树形结构文件窗口 多窗口
- 排序binary tree
- eclipse C/C++ 问题(launch failed,binary not found)
- launch failed.Binary not found in Linux/Ubuntu解决方案
- python递归实现Easyui combotree树
- InnoDB--------查询IOT B+ Tree的高度