leetcode Minimum Depth of Binary Tree
来源:互联网 发布:淘宝质量问题如何投诉 编辑:程序博客网 时间:2024/04/30 21:48
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.
就是对树进行遍历,每次深度+1,当左右节点都为空的时候表示到达了叶节点,比较如果比当前的min小,则记录。
/** * 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:void dfs(TreeNode* root, int &min, int dep){if (root == NULL) return;if (root->left == NULL && root->right == NULL){if (min > dep) min = dep;return;}dfs(root->left, min, dep + 1);dfs(root->right, min, dep + 1);}int minDepth(TreeNode* root) { if (!root) return 0;int min = 9999;dfs(root, min, 1);return min;}};
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
- [Jackson] 使用ObjectMapper对含有任意key的JSON进行反序列化
- 【麦可网】Cocos2d-X跨平台游戏开发学习笔记---第二十四课:Cocos2D-X物理引擎之Box2D1-7
- Google-Guice入门介绍
- eXtremeDB 中索引字段不能为空
- 算法与数据结构八日谈之五——数学相关
- leetcode Minimum Depth of Binary Tree
- Ipython
- 面试题准备(01)-反转句子中单词的顺序
- Java 位操作
- 大数据环境下集成R语言的数据挖掘系统 之 数据分析
- H264中的SPS、PPS提取与作用
- hdoj 3440 House Man 【差分约束】 【经典建模】
- Fragment的简单使用
- 时区间时间的转换