LintCode: Maximum Depth of Binary Tree
C++
1 /** 2 * Definition of TreeNode: 3 * class TreeNode { 4 * public: 5 * int val; 6 * TreeNode *left, *right; 7 * TreeNode(int val) { 8 * this->val = val; 9 * this->left = this->right = NULL; 10 * } 11 * } 12 */ 13 class Solution { 14 public: 15 /** 16 * @param root: The root of binary tree. 17 * @return: An integer 18 */ 19 int maxDepth(TreeNode *root) { 20 // write your code here 21 if (root == NULL) { 22 return 0; 23 } 24 return max(maxDepth(root->left), maxDepth(root->right)) + 1; 25 } 26 };
找我内推: 字节跳动各种岗位
作者:
ZH奶酪(张贺)
邮箱:
cheesezh@qq.com
出处:
http://www.cnblogs.com/CheeseZH/
*
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。