Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
36 changes: 36 additions & 0 deletions 0617-merge-two-binary-trees/altanative-solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* 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:
TreeNode* mergeTrees(TreeNode* root1, TreeNode* root2) {
if (!root1 && !root2) {
return nullptr;
}

int val1 = root1 ? root1->val : 0;
int val2 = root2 ? root2->val : 0;

TreeNode* node = new TreeNode(val1 + val2);

node->left = mergeTrees(
root1 ? root1->left : nullptr,
root2 ? root2->left : nullptr
);

node->right = mergeTrees(
root1 ? root1->right : nullptr,
root2 ? root2->right : nullptr
);

return node;
}
};
11 changes: 11 additions & 0 deletions 0617-merge-two-binary-trees/memo.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
### step1

どう再帰させたらいいかイメージしながら実装しました。

### step2

無駄な部分をChatGPTにスッキリ書き直してもらいました。

### step3

3回通すまで書き直し。
30 changes: 30 additions & 0 deletions 0617-merge-two-binary-trees/step1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* 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:
TreeNode* mergeTrees(TreeNode* root1, TreeNode* root2) {
if (root1 == nullptr && root2 == nullptr) {
return nullptr;
} else if (root1 == nullptr) {
root1 = root2;
return root1;
} else if (root2 == nullptr) {
root2 = root1;
return root2;
} else {
root1->val += root2->val;
root1->left = mergeTrees(root1->left, root2->left);
root1->right = mergeTrees(root1->right, root2->right);
return root1;
}
}
};
28 changes: 28 additions & 0 deletions 0617-merge-two-binary-trees/step2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* 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:
TreeNode* mergeTrees(TreeNode* root1, TreeNode* root2) {
if (!root1) {
return root2;
}
if (!root2) {
return root1;
}

root1->val += root2->val;
root1->left = mergeTrees(root1->left, root2->left);
root1->right = mergeTrees(root1->right, root2->right);

return root1;
}
};
27 changes: 27 additions & 0 deletions 0617-merge-two-binary-trees/step3.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* 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:
TreeNode* mergeTrees(TreeNode* root1, TreeNode* root2) {
if (!root1) {
return root2;
}
if (!root2) {
return root1;
}

root1->val += root2->val;
root1->left = mergeTrees(root1->left, root2->left);
root1->right = mergeTrees(root1->right, root2->right);
return root1;
}
};
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

まず、入力を壊すものであることは注意が必要で、コメントがあると嬉しいかなと思います。
root1の値を更新しているところです。

あとは、root1がないときはroot2を返しているところは、root2の一部を出力が含んでいるので、出来上がった木からもroot2からも、一部書き換えができてしまいます。

入力を壊さずに新しいノードを作っていくという方法もあります。この場合は、片方のノードがないと不便(valどうし足し算できない)なので、ダミーのノードを用意しておくというやり方もあります。

コメント集にいろいろ載っていると思います。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

コメントありがとうございます!
コメントを参考に、非破壊版のコードをalternative-solution.cppで追加してみました。