LeetCode 104: Maximum Depth of Binary Tree 题解

来源:互联网 发布:车辆轨迹 大数据分析 编辑:程序博客网 时间:2024/06/05 06:35

这道题非常简单:

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.


通过代码:

// Cint maxDepth(struct TreeNode* root) {    if(!root) return 0;    int l = maxDepth(root->left);    int r = maxDepth(root->right);    return 1 + (l > r ? l : r);}// C++int maxDepth(struct TreeNode* root) {    if(!root) return 0;    return max(maxDepth(root->left), maxDepth(root->right)) + 1;}
0 0
原创粉丝点击