leetcode Maximum Depth of Binary Tree

来源:互联网 发布:简单好学的软件 编辑:程序博客网 时间:2024/06/05 16:33

找到二叉树中的最大深度

递归方法

对于某一结点来说,分别找到其左右子树的最大深度,则此节点的最大深度为左右子树的较大者+1,加1为包含该节点所在的这一层。

代码

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

0 0
原创粉丝点击