104. Maximum Depth of Binary Tree

来源:互联网 发布:href javascript 编辑:程序博客网 时间:2024/06/06 07:46

递归解法:

/** * Definition for a binary tree node. * 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){            return 0;        }        int max = 0;        int leftDepth = 0;        int deep1 = maxDepth(root->left);        int deep2 = maxDepth(root->right);        return deep1 > deep2? deep1+1:deep2+1;    }};

点击打开链接

0 0
原创粉丝点击