Skip to content

Instantly share code, notes, and snippets.

@walkingtospace
Created August 23, 2014 14:08
Show Gist options
  • Save walkingtospace/751d4ded56df19181ce4 to your computer and use it in GitHub Desktop.
Save walkingtospace/751d4ded56df19181ce4 to your computer and use it in GitHub Desktop.
maximum-depth-of-binary-tree
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