Leetcode | Minimum/Maximum Depth of Binary Tree
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.
BFS碰到一个叶子结点就可以了。
1 class Solution { 2 public: 3 int minDepth(TreeNode *root) { 4 if (root == NULL) return NULL; 5 6 queue<TreeNode*> q; 7 q.push(root); 8 q.push(NULL); 9 int h = 1; 10 while (q.size() > 1) { 11 TreeNode* p = q.front(); 12 q.pop(); 13 14 if (p == NULL) { h++; q.push(NULL); continue;} 15 if (p->left == NULL && p->right == NULL) break; 16 if (p->left) q.push(p->left); 17 if (p->right) q.push(p->right); 18 } 19 return h; 20 } 21 22 };
这里用个NULL指针作哨兵,作为层的结束标志。所有遍历完时,q.size() == 1(q里面只有NULL一个点)。 不过这里因为只要到达叶子结点就会退出,所以不存在死循环的问题。
第三次,bugfree一遍通过。
1 class Solution { 2 public: 3 int minDepth(TreeNode *root) { 4 if (root == NULL) return 0; 5 vector<vector<TreeNode*> > layers(2); 6 int cur = 0, next = 1, layer = 1; 7 layers[cur].push_back(root); 8 while (!layers[cur].empty()) { 9 layers[next].clear(); 10 for (auto node: layers[cur]) { 11 if (node->left == NULL && node->right == NULL) return layer; 12 if (node->left) layers[next].push_back(node->left); 13 if (node->right) layers[next].push_back(node->right); 14 } 15 cur = !cur, next = !next; 16 layer++; 17 } 18 return layer; 19 } 20 };
Maximum Depth of Binary Tree
同样是用bfs好记录层数,然后bfs结束返回值就行了。
1 class Solution { 2 public: 3 int maxDepth(TreeNode *root) { 4 if (root == NULL) return 0; 5 vector<vector<TreeNode*> > layers(2); 6 int cur = 0, next = 1, layer = 0; 7 layers[cur].push_back(root); 8 while (!layers[cur].empty()) { 9 layers[next].clear(); 10 for (auto node: layers[cur]) { 11 if (node->left) layers[next].push_back(node->left); 12 if (node->right) layers[next].push_back(node->right); 13 } 14 cur = !cur, next = !next; 15 layer++; 16 } 17 return layer; 18 } 19 };
转载于:https://www.cnblogs.com/linyx/p/3703449.html
Leetcode | Minimum/Maximum Depth of Binary Tree相关推荐
- LeetCode: 104. Maximum Depth of Binary Tree
题目 Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the l ...
- leetcode 104. Maximum Depth of Binary Tree
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...
- LeetCode之Maximum Depth of Binary Tree
1.题目 Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the ...
- [swift] LeetCode 104. Maximum Depth of Binary Tree
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...
- LeetCode:Minimum Depth of Binary Tree,Maximum Depth of Binary Tree
LeetCode:Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth ...
- 【LeetCode】104. Maximum Depth of Binary Tree (2 solutions)
Maximum Depth of Binary Tree Given a binary tree, find its maximum depth. The maximum depth is the ...
- 【LeetCode 剑指offer刷题】树题4:104 Maximum Depth of Binary Tree
[LeetCode & 剑指offer 刷题笔记]目录(持续更新中...) 104. Maximum Depth of Binary Tree Given a binary tree, fin ...
- LeetCode——Maximum Depth of Binary Tree
LeetCode--Maximum Depth of Binary Tree Question Given a binary tree, find its maximum depth. The max ...
- LeetCode 104. Maximum Depth of Binary Tree--二叉树高度--递归或迭代--C++,Python解法
题目地址:Maximum Depth of Binary Tree - LeetCode Given a binary tree, find its maximum depth. The maximu ...
最新文章
- 原 EOS智能合约开发入门
- 2019第十二届“认证杯”数学建模(第二阶段)
- phpmailer 发送邮件空隙太大_WordPress纯代码无插件开启SMTP邮件服务——墨涩网
- mysql之调优概论
- HDU 2187 悼念512汶川大地震遇难同胞——老人是真饿了
- 如何判断是linux/windows库,module或程序debug还是release(转)
- html ready 调用函数,Chrome和JQuery问题 - $(document).ready(function(){});在页面加载之前调用...
- 西部数码linux云服务器,linux云服务器选择哪个版本
- 北理工计算机 应用基础在线作业,16秋北理工《计算机应用基础》在线作业
- ecshop备份数据 ecshop转移数据 ecshop更换主机
- Python爬虫(十二)_XPath与lxml类库
- PE格式第八讲,TLS表(线程局部存储)
- 整理了两天!B站最全Java学习视频和学习路线
- Linux 基础命令(九)—— 逻辑卷管理(LVM)
- 佳博打印机如何设置热敏打印
- 华为充电协议_华为推出超级快充多协议充电器,支持65W USB PD快充输出
- LZY的CQU水下机器人视觉学习笔记(一)
- 我背透了这些前端八股文
- hao643.com劫持(修改快捷方式跳转至hao123.com)
- 常用数学符号的读法及意义
热门文章
- matlab智能小车避障,Arduino智能小车系列教程4——超声波避障
- FPGA之道(82)功能仿真之仿真原理
- Linux中的基础和小工具
- Amazon Corretto技术细节探秘
- Laravel5.5重写实现未通过认证(多用户)跳转相应登陆页面
- SVN使用_获取某版本后改动的文件列表
- 转义序列Escape Sequences及Linux echo命令多种颜色显示
- listview改变选中行字体颜色
- CodeIgniter 的购物车类只能添加10个商品的解决办法
- sp_executesql 使用复杂的 Unicode 表达式