111. Minimum Depth of Binary Tree(DFS)
来源:互联网 发布:数控编程招聘的视频 编辑:程序博客网 时间:2024/06/05 04:40
1. Description
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.
2. Code
题目比较简单,与寻找最大depth类似。逐层寻找叶节点,第一次找到的那个就是最小深度的叶节点。记录一下代码而已。
/** * 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 == NULL) return 0; if(root->left == NULL && root->right == NULL) return 1; TreeNode * node = NULL; int level = 0; queue<TreeNode*> tmp; tmp.push(root); while( !tmp.empty()) { level ++; for(int i = 0, size = tmp.size(); i < size; i++) { node = tmp.front(); tmp.pop(); if(node->left == NULL && node->right == NULL) return level; if(node->left != NULL) { tmp.push(node->left); } if(node->right != NULL) { tmp.push(node->right); } } } return level; }};
阅读全文
0 0
- DFS: 111. Minimum Depth of Binary Tree
- 111. Minimum Depth of Binary Tree(DFS)
- [LeetCode] Minimum Depth of Binary Tree - DFS
- leetcode dfs Minimum Depth of Binary Tree
- Leetcode 111 - Minimum Depth of Binary Tree(dfs)
- LeetCode 111 Minimum Depth of Binary Tree(DFS)
- [leetcode][tree][dfs] Minimum Depth of Binary Tree
- LeetCode 111. Minimum Depth of Binary Tree
- [LeetCode]111.Minimum Depth of Binary Tree
- 【LeetCode】111.Minimum Depth of Binary Tree
- [Leetcode] 111. Minimum Depth of Binary Tree
- 111.Minimum Depth of Binary Tree
- 111. Minimum Depth of Binary Tree
- [leetcode] 111. Minimum Depth of Binary Tree
- 111. Minimum Depth of Binary Tree
- 111. Minimum Depth of Binary Tree
- 111. Minimum Depth of Binary Tree
- 111. Minimum Depth of Binary Tree LeetCode
- ReactNative基础(二)了解组件的生命周期
- bootstrap table当前行 上移 下移
- python爬虫由浅入深9---定向爬取股票数据信息并保存至本地文件
- FastDFS上传文件二 : 多文件上传
- java 缓存架构剖析--本地缓存(LoadingCache)
- 111. Minimum Depth of Binary Tree(DFS)
- java设计模式(工厂模式)
- ubuntu16.04+cuda8.0安装
- Kubernetes之健康检查与服务依赖处理
- windows强制关闭Tomcat
- 关于正常启动虚拟机的环境下,本机通过无线上外网
- 《并发编程》--22.死锁产生的条件和动静态避免死锁
- chap2 变量和基本类型
- MapReduce1的工作原理