Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions maximum-depth-of-binary-tree/crumbs22.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include <algorithm>

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 maxDepth(TreeNode* root) {
if (!root)
return (0);
return (getMaxDepth(root));
}

int getMaxDepth(TreeNode* node, int depth = 0) {
if (!node)
return (depth);
return std::max(getMaxDepth(node->left, depth + 1), getMaxDepth(node->right, depth + 1));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

depth를 전달하는 메소드를 새로 만드는 것보다 기존의 maxDepth를 활용해서 맨 아래의 node에서 +1을 하여서 올라오는 재귀로 만들면 메모리 용량도 줄이고 조금 더 간단한 코드를 만들 수 있을 것 같네요

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

좋은 리뷰 감사합니다 ! 확실히 불필요한 코드인 것 같아요. 기존 함수 활용하는 방향으로 수정해보도록 하겠습니다! 👍

}
};
44 changes: 44 additions & 0 deletions merge-two-sorted-lists/crumbs22.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include <iostream>

using namespace std;

struct ListNode {
int val;
ListNode *next;
ListNode() : val(0), next(nullptr) {}
ListNode(int x) : val(x), next(nullptr) {}
ListNode(int x, ListNode *next) : val(x), next(next) {}
};

class Solution {
public:
ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) {
if (!list1)
return (list2);
if (!list2)
return (list1);

ListNode ans_head = ListNode();
ListNode* tmp = &ans_head;

while (list1 && list2) {
if (list1->val <= list2->val) {
tmp->next = list1;
list1 = list1->next;
tmp = tmp->next;
}
else {
tmp->next = list2;
list2 = list2->next;
tmp = tmp->next;
}
}
if (list1) {
tmp->next = list1;
}
else if (list2) {
tmp->next = list2;
}
return (ans_head.next);
}
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

c++ 코드는 오랜만에 보는데 java와 다르게 포인터를 써서 주소값을 tmp에 넣어서 하는 것이 차이점으로 보이네요!