Skip to content

Commit b9f5150

Browse files
committed
#227 maximum-depth-of-binary-tree
1 parent d58e437 commit b9f5150

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
ํ’€์ด :
3+
dfs๋ฅผ ์ด์šฉํ•ด ๋ชจ๋“  ๋…ธ๋“œ๋ฅผ ํƒ์ƒ‰ํ•˜๋ฉฐ ๊ฐ€์žฅ ํ•˜์œ„๋…ธ๋“œ์—์„œ ๋ฐ”ํ…€์—… ๋ฐฉ์‹์œผ๋กœ depth๋ฅผ 1์”ฉ ์Œ“์•„๋‚˜๊ฐ€์„œ
4+
์ตœ์ƒ์œ„ ๋…ธ๋“œ์˜ depth๋ฅผ ๊ตฌํ•œ๋‹ค
5+
6+
๋…ธ๋“œ์˜ ๊ฐœ์ˆ˜ : N
7+
8+
TC : O(N)
9+
์ „์ฒด ๋…ธ๋“œ๋ฅผ ์ˆœํšŒํ•˜๋ฏ€๋กœ O(N)
10+
11+
SC : O(N)
12+
์žฌ๊ท€ํ˜ธ์ถœ ์Šคํƒ์ด ๋…ธ๋“œ ๊ฐœ์ˆ˜๋งŒํผ ์Œ“์ด๋ฏ€๋กœ O(N)
13+
*/
14+
15+
/**
16+
* Definition for a binary tree node.
17+
* struct TreeNode {
18+
* int val;
19+
* TreeNode *left;
20+
* TreeNode *right;
21+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
22+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
23+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
24+
* };
25+
*/
26+
class Solution {
27+
public:
28+
int maxDepth(TreeNode* root) {
29+
if (!root)
30+
return 0;
31+
return max(maxDepth(root->left) + 1, maxDepth(root->right) + 1);
32+
}
33+
};

0 commit comments

Comments
ย (0)