[LeetCode104]Maximum Depth of Binary Tree
来源:互联网 发布:松岗卓翼科技工资算法 编辑:程序博客网 时间:2024/04/28 17:20
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.
这道题我用的是递归,如果树的根节点为空,则返回0,否则返回1加上左子树的最大深度和右子树的最大深度中较大的那一个。虽然思路比较直接,代码比较简单,但因为是递归调用,时间复杂度和空间复杂度都比较高。
/** * 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) { if(root==NULL) return 0; else { int Ldepth=maxDepth(root->left)+1; int Rdepth=maxDepth(root->right)+1; return Ldepth>Rdepth?Ldepth:Rdepth; } }};
0 0
- LeetCode104 - Maximum Depth of Binary Tree
- LeetCode104:Maximum Depth of Binary Tree
- [LeetCode104]Maximum Depth of Binary Tree
- leetcode104---Maximum Depth of Binary Tree
- leetcode104 Maximum Depth of Binary Tree
- LeetCode104—Maximum Depth of Binary Tree
- [LeetCode104]Maximum Depth of Binary Tree
- Leetcode104: Maximum Depth of Binary Tree
- leetcode104: Maximum Depth of Binary Tree
- LeetCode104:Maximum Depth of Binary Tree
- LeetCode104. Maximum Depth of Binary Tree
- leetcode104 Maximum Depth of Binary Tree
- leetcode104. Maximum Depth of Binary Tree
- LeetCode104 Maximum Depth of Binary Tree
- LeetCode104. Maximum Depth of Binary Tree
- leetcode104. Maximum Depth of Binary Tree
- LeetCode104. Maximum Depth of Binary Tree
- python--leetcode104. Maximum Depth of Binary Tree
- 运算符重载
- 4_7计数
- MySQL事件Event
- 指针铁律2/3:间接赋值是指针存在的最大意义
- Line Reflection
- [LeetCode104]Maximum Depth of Binary Tree
- 【ES6】对象的扩展2
- 唐灵波_2015级精英班_2015~2016第一学期总结
- Timer和TimerTask总结
- 配置nagios监控oracle,check_oracle_health
- 基于Netty 的webSocket开发
- HTTP协议1
- 网络编程(8)—— IP地址和域名之间的转换
- 有些东西再忙也要做