leetcode 104 二叉树的最大深度 (Maximum Depth of Binary Tree)

/**
 * 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!=NULL){
            return 1+max(maxDepth(root->left),maxDepth(root->right));
        }
        else{
            return 0;
        }
    }
};

猜你喜欢

转载自www.cnblogs.com/azureice/p/leetcode104.html