Skip to content

Commit 4d795f0

Browse files
committed
잘못 올린 풀이 수정
1 parent a60bfb0 commit 4d795f0

1 file changed

Lines changed: 18 additions & 26 deletions

File tree

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,30 @@
11
/**
2-
* Definition for singly-linked list.
3-
* struct ListNode {
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
44
* int val;
5-
* ListNode *next;
6-
* ListNode() : val(0), next(nullptr) {}
7-
* ListNode(int x) : val(x), next(nullptr) {}
8-
* ListNode(int x, ListNode *next) : val(x), next(next) {}
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) {}
910
* };
1011
*/
1112
class Solution {
1213
public:
13-
ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) {
14-
ListNode dummy; // head 초기화의 복잡성을 낮추기 위한 더미노드
15-
ListNode* tail = &dummy;
14+
int maxDepth(TreeNode* root) {
15+
return solve(root, 0);
16+
}
1617

17-
while (list1 != nullptr && list2 != nullptr)
18+
int solve(TreeNode* root, int depth)
19+
{
20+
if (root == nullptr)
1821
{
19-
if (list1->val < list2->val)
20-
{
21-
tail->next = list1;
22-
list1 = list1->next;
23-
}
24-
else
25-
{
26-
tail->next = list2;
27-
list2 = list2->next;
28-
}
29-
30-
tail = tail->next;
22+
return depth;
3123
}
3224

33-
tail->next = (list1 == nullptr) ? list2 : list1;
25+
int l = solve(root->left, depth);
26+
int r = solve(root->right, depth);
3427

35-
return dummy.next;
28+
return max(l, r) + 1;
3629
}
37-
}
38-
;
30+
};

0 commit comments

Comments
 (0)