求二叉树的最大深度 Maximum Depth of Binary Tree

来源:互联网 发布:c语言三阶幻方的判断 编辑:程序博客网 时间:2024/05/16 14:08

题目源自于Leetcode,简单递归题。时间复杂度为O(N),空间复杂度为O(logN)。

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

原创粉丝点击