Easy-17

xiaoxiao2021-02-27  1.6K+

leetcode   104. Maximum Depth of Binary Tree

Given a binary tree, find its maximum depth.

The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.

AC:

/**  * 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 ld=0;     int rd=0;     if(root->left!=NULL)     {         ld=maxDepth(root->left);     }     if(root->right!=NULL)     {         rd=maxDepth(root->right);     }     return ld>rd?ld+1:rd+1; }

转载请注明原文地址: https://www.6miu.com/read-9.html

最新回复(0)