LeetCode——Minimum Depth of Binary Tree
来源:互联网 发布:黑暗骑士 知乎 编辑:程序博客网 时间:2024/04/18 14:28
题目:
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 minDepth(TreeNode* root) { if (root == NULL) { return 0; } stack<TreeNode*> nodeStack; stack<int> deptStack; int curMinDepth = INT_MAX; nodeStack.push(root); deptStack.push(1); while (!nodeStack.empty()) { TreeNode* top = nodeStack.top(); int curDept = deptStack.top(); nodeStack.pop(); deptStack.pop(); if (top->left == NULL && top->right == NULL) { curMinDepth = curMinDepth > curDept ? curDept : curMinDepth; } if (curDept < curMinDepth) { if (top->left) { nodeStack.push(top->left); deptStack.push(curDept + 1); } if (top->right) { nodeStack.push(top->right); deptStack.push(curDept + 1); } } } return curMinDepth; }};
0 0
- LeetCode——Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree——LeetCode(Easy)
- LeetCode——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
- LeetCode111—Minimum Depth of Binary Tree
- LeetCode: Minimum Depth of Binary Tree
- [Leetcode] Minimum Depth of Binary Tree
- LeetCode : Minimum Depth of Binary Tree
- leetcode 31: Minimum Depth of Binary Tree
- [LeetCode] Minimum Depth of Binary Tree - BFS
- [LeetCode] Minimum Depth of Binary Tree - DFS
- 【leetcode】Minimum Depth of Binary Tree
- LeetCode:Minimum Depth of Binary Tree
- [LeetCode]Minimum Depth of Binary Tree
- [Leetcode]Minimum Depth of Binary Tree
- [leetcode]Minimum Depth of Binary Tree
- string实现和使用说明
- JavaScript 运行机制详解:Event Loop
- Java 习惯用法总结
- UVALive_6886_Golf Bot(FFT快速傅里叶变换)
- Docker常用命令
- LeetCode——Minimum Depth of Binary Tree
- bower解决js的依赖管理
- PATBasic——1004. 成绩排名 (20)
- Windows7集成SP1微软原版光盘镜像下载大全
- 第16周项目1异常处理求平方根
- 搜狗语音云开发入门--移动端轻松添加高大上的语音识别
- 织梦后台文章发布时间为1970
- 黑马程序员——Java基础——IO输入与输出_3
- 关于职业发展