Created
August 23, 2014 14:08
-
-
Save walkingtospace/751d4ded56df19181ce4 to your computer and use it in GitHub Desktop.
maximum-depth-of-binary-tree
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
https://oj.leetcode.com/problems/maximum-depth-of-binary-tree/ | |
/** | |
* Definition for binary tree | |
* 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 maxDepth(root, 0); | |
} | |
int maxDepth(TreeNode* node, int depth) { | |
if(node == NULL) return depth; | |
return max(maxDepth(node->left, depth+1), maxDepth(node->right, depth+1)); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment