111:Minimum Depth of Binary Tree【树】【DFS】
来源:互联网 发布:淘宝菜鸟驿站没有了 编辑:程序博客网 时间:2024/06/06 05:31
题目链接:click~
/*题意:求二叉树的最小深度(从根结点到最近的叶子结点)*//** *思路:DFS遍历整个树,只需要叶子结点的到根的距离 * */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;//叶子结点 int LeftDepth = minDepth(root->left); //左子树的最小深度 int RightDepth = minDepth(root->right);//右子树的最小深度 if(LeftDepth == 0) //非叶子结点且左子树为空 return RightDepth + 1; else if(RightDepth == 0) //非叶子结点且右子树为空 return LeftDepth + 1; else return min(LeftDepth, RightDepth) + 1; }};
0 0
- 111:Minimum Depth of Binary Tree【树】【DFS】
- [LeetCode] Minimum Depth of Binary Tree - DFS
- leetcode dfs Minimum Depth of Binary Tree
- DFS: 111. Minimum Depth of Binary Tree
- LeetCode 111 Minimum Depth of Binary Tree (DFS)
- 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: Minimum Depth of Binary Tree [111]
- LeetCode 111 Minimum Depth of Binary Tree
- [leetcode 111] Minimum Depth of Binary Tree
- 111Minimum Depth of Binary Tree
- 111Minimum 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
- Activity的显式意图激活和隐式意图激活
- 网络游戏服务器构架设计-刀剑Online
- Effective C++读书笔记
- c#第二次作业【图片随机浏览器】
- Cross-validation(交叉验证)
- 111:Minimum Depth of Binary Tree【树】【DFS】
- 最大流最小割算法入门理解
- c++ 常用库
- 单反相机参数之光圈、快门篇
- android5.0官网编译环境搭建(Initializing a Build Environment)
- memcache熟悉步骤
- Dijkstra复习
- 医疗软件实施入门02
- 《C++Primer》读书笔记(5)