LeetCode 111 Minimum Depth of Binary Tree(DFS)
来源:互联网 发布:诺基亚500软件 编辑:程序博客网 时间:2024/05/22 15:58
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.
题目大意:给出一个二叉树,求从根节点到叶节点的最小深度。
解题思路:直接DFS,和LeetCode 104 Maximum Depth of Bianry Tree类似,只不过多了些判断。
代码如下:
/** * Definition for a binary tree node. * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */int minDepth(struct TreeNode* root) { if(root == NULL) return 0; else if(root->left && !root->right) return 1 + minDepth(root->left); else if(root->right && !root->left) return 1 + minDepth(root->right); else{ int left = 1 + minDepth(root->left); int right = 1 + minDepth(root->right); return left < right ? left : right; }}
阅读全文
0 0
- Leetcode 111 - Minimum Depth of Binary Tree(dfs)
- LeetCode 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][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
- [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
- 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
- Dijkstra算法讲解(单源最短路径问题求解)
- ts或264通过RTP发送
- html5在手机端调用摄像头的介绍以及新特性
- 使用IntelliJ IDEA开发JavaWeb项目
- 凡是属性中有Set的都直接声明中直接new出来
- LeetCode 111 Minimum Depth of Binary Tree(DFS)
- 数据库杂谈之:如何优雅的进行表结构设计
- 拉链法
- Android 7.0 行为变更
- Chapter3.1.1 python函数小示例
- Google I O 2017 之Android开发特性
- urlconnection示例
- caffe GPU版本使用fcn分割自己的图像
- html img图片不变形等比例缩放,兼容ie6