[LeetCode] Maximum Depth of Binary Tree
来源:互联网 发布:xnview mac 破解 编辑:程序博客网 时间:2024/05/23 09:59
这道题主要想说明频繁的函数调用时非常耗时的。
/** * 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 maxDepth(TreeNode *root) { if( !root ) return 0; else{ int maxDepthLeft = maxDepth( root->left ); int maxDepthRight = maxDepth( root->right ); return (maxDepthLeft>maxDepthRight) ? (maxDepthLeft+1) : (maxDepthRight+1); } }};
/** * 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 maxDepth(TreeNode *root) { if( !root ) return 0; else{ int maxDepthLeft = maxDepth( root->left ); int maxDepthRight = maxDepth( root->right ); return max(maxDepthLeft, maxDepthRight) + 1; } }};
0 0
- Leetcode - Tree - Maximum Depth of Binary Tree
- [leetcode][tree] Maximum Depth of Binary Tree
- LeetCode Maximum Depth of Binary Tree
- [Leetcode] Maximum Depth of Binary Tree
- leetcode 24: Maximum Depth of Binary Tree
- [LeetCode] Maximum Depth of Binary Tree
- Leetcode 104 Maximum Depth of Binary Tree
- 【leetcode】Maximum Depth of Binary Tree
- [leetcode]Maximum Depth of Binary Tree
- [LeetCode]Maximum Depth of Binary Tree
- [leetcode]Maximum Depth of Binary Tree
- Leetcode: Maximum Depth of Binary Tree
- LeetCode-Maximum Depth of Binary Tree
- [leetcode] Maximum Depth of Binary Tree
- LeetCode - Maximum Depth of Binary Tree
- LeetCode:Maximum Depth of Binary Tree
- LeetCode | Maximum Depth of Binary Tree
- 【leetcode】Maximum Depth of Binary Tree
- Wampserver配置方法
- bzoj 2006
- OC 中的方法重载?
- 独立成分分析http://blog.sina.com.cn/s/blog_73402e3c0101gqy0.html
- $.ajax()方法详解
- [LeetCode] Maximum Depth of Binary Tree
- 解决:生成api文档出现的编码GBK的不可映射字符的问题
- 结巴分词源代码解析(二)
- 记录一些未解决的问题
- show bash version
- Linux进程控制命令之ps
- 黑马程序员——动态内存
- bash regrex operator =~
- OC视频笔记3.6(字符串常用方法)3.7(可变字符串的常用方法)