Maximum Depth of Binary Tree

来源:互联网 发布:如何学英语 知乎 编辑:程序博客网 时间:2024/04/27 07:30
/** * Definition for binary tree * struct TreeNode { *     int val; *     TreeNode *left; *     TreeNode *right; *     TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public:    int maxDepth(TreeNode *root) {        if(root==NULL)        {            return 0;        }        int ldepth=maxDepth(root->left);        int rdepth=maxDepth(root->right);        return ldepth>rdepth?ldepth+1:rdepth+1;    }};

0 0
原创粉丝点击