111. Minimum Depth of Binary Tree & 104. Maximum Depth of Binary Tree
来源:互联网 发布:淘宝怎么看访客来源 编辑:程序博客网 时间:2024/05/18 03:34
111. Minimum Depth of Binary Tree
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) return 0; int left = minDepth(root->left); int right = minDepth(root->right); if(right == 0 || left == 0){ return 1 + left + right; }else { return 1+min(left,right); } }};
104. Maximum Depth of Binary Tree
Given a binary tree, find its maximum depth.
The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
class Solution {public: int maxDepth(TreeNode* root) { if(!root) return 0; int left = maxDepth(root->left); int right = maxDepth(root->right); if(right == 0 || left == 0){ return 1 + left + right; }else { return 1+max(left,right); } }};
0 0
- 111. Minimum & Maximum Depth of Binary Tree
- 101. Symmetric Tree\104. Maximum Depth of Binary Tree\111. Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree,Maximum Depth of Binary Tree
- 111. Minimum Depth of Binary Tree & 104. Maximum Depth of Binary Tree
- LeetCode 104. Maximum Depth of Binary Tree和111. Minimum Depth of Binary Tree 递归
- 104. Maximum Depth of Binary Tree 111. Minimum Depth of Binary Tree
- Minimum(Maximum) Depth of Binary Tree
- Maximum & Minimum Depth of Binary Tree
- [Leetcode] Maximum |Minimum Depth of Binary Tree
- LeetCode-Maximum/Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- 使用opencv训练自己的检测器(2)
- HUST
- 知识的管理会比知识的存储更重要
- R语言的二分法和knn算法
- malloc原理和内存分配
- 111. Minimum Depth of Binary Tree & 104. Maximum Depth of Binary Tree
- tomcat容器等级
- 蓝桥杯加法变乘法
- 剑指Offer 39 二叉树的深度+平衡二叉树
- getParameter() getInputStream()和getReader() 区别
- C#学习日记 队列 栈
- 23种设计模式09---桥接(Bridge)模式
- Fibonacci数
- mysql编程