【LeetCode从零单刷】Minimum Depth of Binary Tree
来源:互联网 发布:淘宝详情页切片大小 编辑:程序博客网 时间:2024/06/05 16:07
题目:
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.
解答:
非常无聊的一题。需要注意的是:叶子节点是自身不为 NULL,但是左子树与右子树同时为 NULL 的节点。
所以,只要左右子树任意一棵不为 NULL,寻找 depth 的过程就不能停止。
/** * 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; if (root->left == NULL && root->right != NULL) return minDepth(root->right) + 1; if (root->right == NULL && root->left != NULL) return minDepth(root->left) + 1; return min( minDepth(root->left), minDepth(root->right) ) + 1; }};
0 0
- 【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 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
- 478B. Random Teams
- msvbvm50.exewin7修复版
- 字符串匹配算法(BF和KMP)
- java
- Java的4种代码块
- 【LeetCode从零单刷】Minimum Depth of Binary Tree
- 【UML学习】九种图的初识
- OC动态绑定
- 简易文本爬虫
- 字符串对比
- 一起talk C栗子吧(第六十六回:C语言实例--DIY字符串比较函数)
- FPGA服务器硬件搭建涉及的知识点1
- oc对象初始化
- 响铃:当创业者选择了一个全新的产业,以及一个二三线城市,该怎么办?