leetcode-111-Minimum Depth of Binary Tree
2015-07-23 22:04
274 查看
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.
求一个二叉树的最小深度,即从头节点到叶子节点的最短距离。
/** * 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 solve(TreeNode* root,int n) { if (!root->left && !root->right) return n + 1;// 到了叶子节点 就返回深度 else if (!root->left && root->right) return solve(root->right,n + 1); // 左节点为空 访问右节点 else if (root->left && !root->right) return solve(root->left,n + 1); // 右节点为空 访问左节点 else { return min(solve(root->left,n + 1),solve(root->right,n + 1)); // 返回左右节点的最小深度 } } int minDepth(TreeNode* root) { if (!root) return 0; return solve(root,0); } };
另外一种递归
/** * 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 solve(TreeNode* root,int n) { if (!root) return 99999; // 为空则 返回一个无穷大的值 if (!root->left && !root->right) return n + 1; // <span style="font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;">到了叶子节点 就返回深度</span> else { return min(solve(root->left,n + 1),solve(root->right,n + 1)); <span style="font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;">// 非叶子节点 返回左右节点的最小深度</span> } } int minDepth(TreeNode* root) { if (!root) return 0; return solve(root,0); } };
非递归求解
用队列实现BSF,层次遍历二叉树,找到有叶子节点所在的层数。 参考 点击打开链接
/** * 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) return 0; int ans = 0; // 层数 即深度 int count_ = 1; // 每一层的节点数 TreeNode* t; queue<TreeNode* > q; q.push(root); int f = 0; while (!q.empty()) { t = q.front(); q.pop(); count_--; if (!t->left&&!t->right) f = 1; // 找到一个深度最小的叶子节点 标记这一层为深度最小的一层 if (t->left) q.push(t->left); if (t->right) q.push(t->right); if (count_ == 0) { // 该层的节点访问完 count_ = q.size(); // 下一层的节点数 ans ++; if (f) break; } } return ans; } };
way 2:
/** * 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) return 0; int ans = 1; // 下一层的层数 int count_ = 1; // 每一层的节点数 TreeNode* t; queue<TreeNode* > q; q.push(root); while (!q.empty()) { t = q.front(); q.pop(); count_--; if (!t->left&&!t->right) break; // 找到一个深度最小的叶子节点 就结束 if (t->left) q.push(t->left); if (t->right) q.push(t->right); if (count_ == 0) { // 该层的节点访问完 count_ = q.size(); // 下一层的节点数 ans ++; } } return ans; } };
今天有想了一下 递归的问题
/** * 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 solve(TreeNode* root) { if (!root) return 0; else if (root->left && !root->right) return 1 + solve(root->left); else if (!root->left && root->right) return 1 + solve(root->right); else return 1 + min(solve(root->right) , solve(root->left)); } int minDepth(TreeNode* root) { return solve(root); } };
相关文章推荐
- 关于PL\SQL无法在64位Client下使用的原因及解决办法
- eclipse git 插件集成与本地使用(傻瓜教程 一)
- JAVA基础之IO流
- 学习CSS(7)
- Java容器类
- Angular2 Router
- 移动端多屏适配方案
- java中Class.forName与new
- 概率DP POJ3071
- Repeater的ItemCommand事件(LinkButton)
- Linux下Nagios的安装与配置
- 控制器
- Android基础之HttpUrlConnection小案例
- 关于PHP程序员解决问题的能力
- Linux kernel 分析之十八:设计模式-文件系统与抽象工厂
- Postgres数据库的进程结构---菜鸟的PG分析之路
- BZOJ 2750: [HAOI2012]Road( 最短路 )
- 什么是树型文件目录结构,它是如何构成的?
- 网络拓扑图例库
- cpp文件调用CUDA .cu文件实现显卡加速相关编程