[leetcode] Minimum Depth of Binary Tree
来源:互联网 发布:网络直播傅园慧 编辑:程序博客网 时间:2024/06/07 08:00
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
代码:
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public: int minDepth(TreeNode *root) { int mindepth; mindepth=100000; if(root==NULL) return 0; dfs(root,1,mindepth); return mindepth; } void dfs(TreeNode *root, int depth, int &mindepth){ if(root==NULL) return; if(root->left==NULL && root->right==NULL){ if(depth<mindepth) mindepth=depth; return; } depth++; dfs(root->left,depth,mindepth); dfs(root->right,depth,mindepth); }};
0 0
- 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
- 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
- LeetCode | Minimum Depth of Binary Tree
- LeetCode - Minimum Depth of Binary Tree
- C++大学基础教程_10_67_new和delete和static
- linux修改最大文件链接数open files/ulimit -n
- C#的接口有什么用?
- 百度首席科学家 Andrew Ng谈深度学习的挑战和未来
- ASP.Net网站管理工具配置
- [leetcode] Minimum Depth of Binary Tree
- cout 以2/8/10/16进制输出
- 淘宝Tair分布式缓存系统 总体结构分析
- 自定义Unity网络播放器的载入画面
- #小城大微# 快来关注小城大微微信平台吧!
- 4复数与复变函数(四)
- 颜色的十六进制代码
- xcode4的环境变量,Build Settings参数,workspace及联编设置
- 面向对象_大纲 by霹雳火毕老师