104. Maximum Depth of Binary Tree

Recursive DFS Solution

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */

int maxDepth(struct TreeNode* root){
    if (root == NULL) {
        return 0;
    }
    
    int a = maxDepth(root->left);
    int b = maxDepth(root->right);
    
    return (a > b ? a : b) + 1;
}

Iterative DFS Solution

TODO

Complexity

  • Time Complexity: O(N), where N is the number of nodes.
  • Space Complexity: O(N), where N is the size of the implicit call stack.

All Solutions