[InterviewBit] Max Depth of Binary Tree

Max Depth of Binary Tree

  • Time :
  • Space :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
int helper(TreeNode* node, int now) {
if(!node) return now;
return max(helper(node->left, now + 1), helper(node->right, now + 1));
}
int Solution::maxDepth(TreeNode* A) {
return A != NULL ? helper(A,0) : 0;
}

Author: Song Hayoung
Link: https://songhayoung.github.io/2022/10/04/PS/interviewbit/max-depth-of-binary-tree/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.