Leetcode 104. Maximum Depth of Binary Tree
求树的最大深度,如果树为空,则返回0
否则返回左最大深度和右最大深度的最大值,进行加一
/** * 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; return max(maxDepth(root->left),maxDepth(root->right)) + 1; } };