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;
        }
    }
};

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325964533&siteId=291194637