leetcode: Minimum Depth of Binary Tree
来源:互联网 发布:论文引用格式网络文章 编辑:程序博客网 时间:2024/06/06 19:29
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.
题目描述:
求二叉树的最小深度。和求二叉树的最大深度有些区别,仔细看了别人的简洁代码。
代码实现:
/** * 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==0) { return 0; } int left=minDepth(root->left); int right=minDepth(root->right); if(left==0) { return right+1; } else if(right==0) { return left+1; } else { return min(left,right)+1; } }};
1 1
- 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
- 面试宝典_JVM
- luaplus和c++之间的代码交互
- 汇编语言的重要性
- adb logcat获取异常日志代码
- PopupWindow实现弹窗效果
- leetcode: Minimum Depth of Binary Tree
- Hibernate知识点
- UIButton设置圆角
- 解决 win32控制台程序一闪而过
- Swift学习笔记(十七)协议
- push和pushl的区别
- ios线程-GCD
- Nginx Proxy cache + Perl + ImageMagick 做Wap站图片适配 实时压缩
- antlib.xml could not create task or type of type componentdef