LeetCode Minimum Depth of Binary Tree
来源:互联网 发布:2016中国汽车销量数据 编辑:程序博客网 时间:2024/06/05 18:10
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 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) { if(root == NULL) return 0; else if(root->left == NULL && root->right == NULL) return 1; else if(root->left != NULL && root->right == NULL) return minDepth(root->left)+1; else if(root->left == NULL && root->right != NULL) return minDepth(root->right)+1; else return min(minDepth(root->left), minDepth(root->right))+1; }private: int min(const int& a, const int& b) { return a < b? a : b; } };
第二次写这个类:
/** * 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) { if(root == NULL) return 0; else if(root->left == NULL) return minDepth(root->right)+1; else if(root->right == NULL) return minDepth(root->left)+1; else return min(minDepth(root->left), minDepth(root->right))+1; }private: int min(const int& a, const int& b) { return a < b? a : b; } };
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
- 深入探讨 java.lang.ref 包
- javascript的一些特性
- PHP FTP操作类( 上传、拷贝、移动、删除文件/创建目录 )
- java isassignablefrom 判断子父类关系
- Python 使用 Xlrd/xlwt 操作 Excel
- LeetCode Minimum Depth of Binary Tree
- Android的三种网络通信方式
- 第二周作业01 -- 素数的算法实现
- Python 学习经历(一) :python语法总结
- Codeforces Round #194 (Div. 2)
- Chrome OS (hexxeh编译版本)安装到硬盘的方法修正
- SmartMio:懒人健身专用,八块腹肌等着你
- 在C程序中内嵌Python解释器,用脚本语言实现灵活的Plugin机制
- Java中final关键字用法总结