LeetCode(104)Maximum Depth of Binary Tree

来源:互联网 发布:网络剧版权价值 编辑:程序博客网 时间:2024/05/22 04:52

题目如下:

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.

题目分析:

简单题。求树的最大深度。递归求解。

class Solution {public:    int maxDepth(TreeNode *root) {        if(root==NULL)            return 0;        else{            int l=maxDepth(root->left);            int r=maxDepth(root->right);            return 1+(l>r?l:r);        }    }};


0 0
原创粉丝点击