【leetcode】104-Maximum Depth of Binary Tree

problem

Maximum Depth of Binary Tree

 code

递归方法

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

参考

1.Maximum Depth of Binary Tree

猜你喜欢

转载自www.cnblogs.com/happyamyhope/p/10019440.html