Leek: 104. The maximum depth of a binary tree

Given a binary tree, find its maximum depth.

The depth of a binary tree is the number of nodes on the longest path from the root node to the farthest leaf node.

Explanation: A leaf node refers to a node without child nodes.

Example:


Given a binary tree [3,9,20,null,null,15,7],

    3
   / \
  9 20
    / \
   15 7
returns its maximum depth of 3 .

Source: LeetCode
Link: https://leetcode.cn/problems/maximum-depth-of-binary-tree
The copyright belongs to Leetcode Network. For commercial reprints, please contact the official authorization, for non-commercial reprints, please indicate the source.

/**
 * 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;
    }
    return fmax(maxDepth(root ->left),maxDepth(root ->right)) + 1;

}

 

Guess you like

Origin blog.csdn.net/SIHUAZERO/article/details/127832410