//minmum-depth-of-binary-tree
//递归深度优先
class Solution {
public:int run(TreeNode *root) {if(root == NULL)return 0;int l = run(root->left);int r = run(root->right);if(l == 0 || r == 0)//有斜树的情况,即根节点的只有左子树有节点return 1 + r + l;return min(l,r) + 1;}
};
//非递归,层次遍历
class Solution {
public:int run(TreeNode *root) {if(root == NULL)return 0;if(root->left == NULL && root->right == NULL)return 1;queue<TreeNode*> qu;TreeNode * t = NULL;int count = 1;qu.push(root);qu.push(NULL);// 每一层入队后以NULL节点标记下while(!qu.empty()){t = qu.front();qu.pop();if(t == NULL){qu.push(NULL);count++;continue;}if(t->left == NULL && t->right == NULL)return count;if(t->left != NULL)qu.push(t->left);if(t->right!= NULL)qu.push(t->right);}return count;}
};
详细解决方案
Leetcode----minmum-depth-of-binary-tree
热度:26 发布时间:2024-01-04 02:09:06.0
相关解决方案
- 怎么将html格式的Excel转换为二进制格式的Excel,即BIFF格式 (Binary File Format))
- asp.net tree view 空件在那下载?解决思路
- 关于 XML 和 javascript 在 asp.net页面显示 tree 的有关问题
- 解决libxml/tree.h not found有关问题
- extJs tree,该怎么处理
- 请教上哪位高手知道,column-tree.css中zoom是什么意思,在上面这代码里面起什么作用
- EXT tree 真么平添单击事件
- 急 求大神帮忙关于jquey easy ui tree,该怎么处理
- Ext tree 优化有关问题
- Ext tree 用节点做左边导航连接,重复点击不刷新(有关问题已自己搞定,有人要分吗)
- Easyui - combo[tree,box]下拉图标有间隙bug解决办法
- Ext4.x 树报表控件【Ext.tree.Panel】 Demo
- Jquery EasyUI tree 怎么定义叶节点
- Ext.tree.TreeNode 树型菜单不能展示
- tree 跟treetable
- easyui运用二――combotree/tree
- Ext-Grid,Tree,Form等总结
- GWT-EXT TREE(Panel)联动滚动条的兑现
- 关于"Source Distribution"跟"Binary Distribution "
- ext js tree 带搜寻(支持树枝节点和叶子节点)+ 大select(mutiple)
- Tree 满载 加载
- [Ext JS 四] 实战之Grid, Tree Gird 动态添加行
- mx:Tree 打开全部节点
- [Ext JS 四] 实战之Grid, Tree Gird 动态添加列
- Add tooltip and . to header and tree in your AdvancedDataGrid
- Ext Tree 加载超时 Timeout 有关问题的解决方法
- extjs tree checkbox 复选框兑现 取值 显示
- EXt tree 简略增、删、改、查
- 责任书Ext.tree.TreeNode在视野中
- Ext.tree.panel中怎么每次点击展开都从后台加载