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; }