Skip to content

Instantly share code, notes, and snippets.

@lishunan246
Created April 11, 2021 08:09
Show Gist options
  • Save lishunan246/7b9e1d79c5da67cea3efbb43f62d7bf1 to your computer and use it in GitHub Desktop.
Save lishunan246/7b9e1d79c5da67cea3efbb43f62d7bf1 to your computer and use it in GitHub Desktop.
剑指 Offer 55 - I. 二叉树的深度
/**
* 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 == nullptr) {
return 0;
} else {
return 1 + std::max(maxDepth(root->left), maxDepth(root->right));
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment