Minimum Depth of Binary Tree
来源:互联网 发布:手机触摸屏控制软件 编辑:程序博客网 时间:2024/06/04 19:37
struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right;};int minDepth(struct TreeNode* root) { if(root==NULL) { return 0; } if(root->left==NULL&&root->right==NULL) { return 1; } int rD=0; int lD=0; rD=minDepth(root->right); lD=minDepth(root->left); if(rD==0) { return lD+1; } else if(lD==0) { return rD+1; } else { return rD>lD?(lD+1):(rD+1); }}
递归还是不懂啊
这边写一下
递归应该是先考虑最一般情况这边就是如果一个root为NULL那么就返回0层
root的左右节点都是NULL 就返回1
否则就进入递归了
看一个root的左右子树的高 输出低的
如果一边是NULL 就为另一边的长度 因为题目要求的应该是从根节点直接到叶节点的长度
递归是不是可以这样考虑
我先想最基本的一些情况 然后再弄一棵大树的一棵子树 并想办法不断加上去?
还要多练练
0 0
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- jQuery选择器
- 数据结构与算法分析 java语言描述
- hdu1312(Red and Black)
- linux将程序放到后台执行
- NodeJS 常用模块整理
- Minimum Depth of Binary Tree
- hdu-4417-Super Mario-只查询的主席树
- HDU 1024 Max Sum Plus Plus((最大m段子段和))(动态规划经典)
- POJ 1129 Channel Allocation(染色)
- 最重要的财富——信用:守信重于生命
- 如何理解挂载点
- 剑指Offer面试题23(Java版):从上往下打印二叉树
- centos6使用 swig3.0.6 编译c,golang 1.4.2调用
- 九度oj 1447