LeetCode刷题(C++)——Maximum Depth of Binary Tree(Easy)
来源:互联网 发布:js promise实现原理 编辑:程序博客网 时间:2024/05/22 07:58
题目描述
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 getDepth(TreeNode* p) { if(p==NULL) { return 0; } int LD,RD; LD = getDepth(p->left); RD =getDepth(p->right); return (LD>RD?LD:RD)+1; } int maxDepth(TreeNode* root) { return getDepth(root); }};
一种更简便的实现方法
/** * 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 maxDepth(TreeNode* root) { return root? max(maxDepth(root->left),maxDepth(root->right))+1:0; }};
1 0
- LeetCode刷题(C++)——Maximum Depth of Binary Tree(Easy)
- Maximum Depth of Binary Tree(easy)
- Minimum Depth of Binary Tree——LeetCode(Easy)
- LeetCode-Easy刷题(21) Maximum Depth of Binary Tree
- [leetcode-104]Maximum Depth of Binary Tree(c)
- LeetCode 104. Maximum Depth of Binary Tree 题解(C++)
- leetCode刷题——Maximum Depth of Binary Tree
- Maximum Depth of Binary Tree——Difficulty:Easy
- 【leetcode】【Easy】【104. Maximum Depth of Binary Tree】【tree】
- [C]LeetCode:Maximum Depth of Binary Tree
- leetcode 104 Maximum Depth of Binary Tree(难易度:Easy)
- [Leetcode 104, Easy] Minimum (Maximum) Depth of Binary Tree
- 【LeetCode】(104)Maximum Depth of Binary Tree(Easy)
- Leetcode----104. Maximum Depth of Binary Tree(easy)
- Leetcode 104. Maximum Depth of Binary Tree (Easy) (cpp)
- LeetCode #104 - Maximum Depth of Binary Tree - Easy
- LeetCode解题报告 104. Maximum Depth of Binary Tree [easy]
- 【Leetcode-easy-104】Maximum Depth of Binary Tree
- 上海面试经历
- java 用redis如何处理电商平台,秒杀、抢购超卖
- android与js互调
- 八九天内容回顾(API.正则表达式...)
- 如何绘制ROC曲线
- LeetCode刷题(C++)——Maximum Depth of Binary Tree(Easy)
- maven错误
- vue.js 过滤器、ajax数据、事件监听实例
- C++引用详解
- JavaScript之命名空间模式
- 页面下拉框的回显
- Java多线程/并发16、Atomic原子变量和原子操作
- J2EE
- ubuntu mysql 字符集设置