面试题55 - I. 二叉树的深度

题目:

 

 

解答:

 1 /**
 2  * Definition for a binary tree node.
 3  * struct TreeNode {
 4  *     int val;
 5  *     TreeNode *left;
 6  *     TreeNode *right;
 7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 8  * };
 9  */
10 class Solution {
11 public:
12     int maxDepth(TreeNode* root) 
13     {
14         if (NULL == root)
15         {
16             return 0;
17         }
18 
19         int leftdepth = maxDepth(root->left);
20         int rightdepth = maxDepth(root->right);
21         
22         return std::max(leftdepth, rightdepth) + 1;
23 
24     }
25 };

 

posted @ 2020-05-09 19:05  梦醒潇湘  阅读(122)  评论(0)    收藏  举报