104. Maximum Depth of Binary Tree LeetCode

来源:互联网 发布:软件研发过程模型 编辑:程序博客网 时间:2024/05/13 05:41

题意:求二叉树的最大深度。
题解:递归求,从根节点出发的最大深度等于从左儿子出发的最大深度+1或者从右儿子出发的最大深度加1.

class Solution {public:    int maxDepth(TreeNode* root) {        if(root == NULL) return 0;        if(root->left == NULL && root->right == NULL) return 1;        int l = 0,r = 0;        if(root->left != NULL) l = maxDepth(root->left);        if(root->right != NULL) r = maxDepth(root->right);        return max(l,r) + 1;    }};
0 0
原创粉丝点击