diff options
author | 3gg <3gg@shellblade.net> | 2025-02-05 18:36:31 -0800 |
---|---|---|
committer | 3gg <3gg@shellblade.net> | 2025-02-05 18:36:31 -0800 |
commit | 4689e4e80b479be25f7557d05818f5caa723aafa (patch) | |
tree | 4df25811fe2a9a15b401375178da6537f4b6063f /top-interview-questions/easy/trees/01_maximum_depth_of_binary_tree.cc |
Diffstat (limited to 'top-interview-questions/easy/trees/01_maximum_depth_of_binary_tree.cc')
-rw-r--r-- | top-interview-questions/easy/trees/01_maximum_depth_of_binary_tree.cc | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/top-interview-questions/easy/trees/01_maximum_depth_of_binary_tree.cc b/top-interview-questions/easy/trees/01_maximum_depth_of_binary_tree.cc new file mode 100644 index 0000000..4d13af7 --- /dev/null +++ b/top-interview-questions/easy/trees/01_maximum_depth_of_binary_tree.cc | |||
@@ -0,0 +1,20 @@ | |||
1 | /** | ||
2 | * Definition for a binary tree node. | ||
3 | * struct TreeNode { | ||
4 | * int val; | ||
5 | * TreeNode *left; | ||
6 | * TreeNode *right; | ||
7 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} | ||
8 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} | ||
9 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} | ||
10 | * }; | ||
11 | */ | ||
12 | class Solution { | ||
13 | public: | ||
14 | int max(int a, int b) { return a > b ? a : b; } | ||
15 | |||
16 | int maxDepth(TreeNode* root) { | ||
17 | if (!root) return 0; | ||
18 | return 1 + max(maxDepth(root->left), maxDepth(root->right)); | ||
19 | } | ||
20 | }; | ||