LeetCode104 - Maximum Depth of Binary Tree

àì夳堔傛蜴生んèń 2022-08-07 08:56 144阅读 0赞

[Q]

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.

[C]

/**
* Definition for binary tree
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
int maxDepth(struct TreeNode *root) {
if (root == NULL) return 0;
int left = maxDepth(root -> left);
int right = maxDepth(root -> right);
return left > right ? left +1 : right +1;
}

发表评论

表情:
评论列表 (有 0 条评论,144人围观)

还没有评论,来说两句吧...

相关阅读