-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path104.cpp
More file actions
34 lines (33 loc) · 1 KB
/
104.cpp
File metadata and controls
34 lines (33 loc) · 1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// Author: btjanaka (Bryon Tjanaka)
// Problem: (LeetCode) 104
// Title: Maximum Depth of Binary Tree
// Link: https://leetcode.com/problems/maximum-depth-of-binary-tree/
// Idea: Recur through the binary tree keeping track of depth.
// Difficulty: Easy
// Tags:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left),
* right(right) {}
* };
*/
class Solution {
public:
int maxDepth2(TreeNode* root, int depth) {
int leftDepth = 0, rightDepth = 0;
if (root->left) {
leftDepth = maxDepth2(root->left, depth + 1);
}
if (root->right) {
rightDepth = maxDepth2(root->right, depth + 1);
}
return max(depth, max(leftDepth, rightDepth));
}
int maxDepth(TreeNode* root) { return root ? maxDepth2(root, 1) : 0; }
};