- class Solution {
- public:
- int maxDepth(TreeNode* root) {
- int ans = 0;
- travel(root, ans, 0);
- return ans;
- }
- void travel(TreeNode* root, int& ans, int length) {
- if (root == NULL) {
- if (length > ans)
- ans = length;
- return ;
- }
- travel(root->left, ans, length + 1);
- travel(root->right, ans, length + 1);
- }
- };