LeetCode | 104. Maximum depth of binary tree

LeetCode | 104. Maximum depth of binary tree

OJ link

Insert image description here

  • One thing to note here is that every time there is a return value, you need to define a variable to save the last value.
  • Finally take the highest oneAdd 1
int maxDepth(struct TreeNode* root) {
    
    
    if(root == NULL)
        return NULL;
    int left = maxDepth(root->left);
    int right = maxDepth(root->right);
    return left > right ? left + 1 : right + 1;
}

Guess you like

Origin blog.csdn.net/2201_76004325/article/details/134704385