Open
Conversation
added 2 commits
September 12, 2024 08:52
| } | ||
| ``` | ||
|
|
||
| これで破壊的な動作はなくなりましたが、どちらも root1, root2 をそのまま返す可能性があるのが気がかりです。問題文には "You need to merge the two trees into a new binary tree" とあるので、これを満たさないと判断されるようでしたらやはり deep copy を実施する関数を自前で実装するなり、そういった関数がある前提で処理を書くことになるのかなと考えました。 |
There was a problem hiding this comment.
deep copyをする関数、そんなに書くの大変じゃないと思うので書いてみても良い気がしました
There was a problem hiding this comment.
こんな感じでどうでしょうか。
class Solution {
public:
TreeNode* mergeTrees(TreeNode* root1, TreeNode* root2) {
// {merged_parent, child1, child2, is_left_child}
stack<tuple<TreeNode*, TreeNode*, TreeNode*, bool>> st;
TreeNode dummy;
st.push({&dummy, root1, root2, true});
while (!st.empty()) {
auto [merged_parent, child1, child2, is_left_child] = st.top();
st.pop();
if (!child1 && !child2) {
continue;
}
auto merged_child = new TreeNode();
TreeNode *left1 = nullptr, *left2 = nullptr,
*right1 = nullptr, *right2 = nullptr;
if (child1) {
merged_child->val += child1->val;
left1 = child1->left;
right1 = child1->right;
}
if (child2) {
merged_child->val += child2->val;
left2 = child2->left;
right2 = child2->right;
}
st.push({merged_child, left1, left2, true});
st.push({merged_child, right1, right2, false});
if (is_left_child) {
merged_parent->left = merged_child;
} else {
merged_parent->right = merged_child;
}
}
return dummy.left;
}
};|
良さそうです! |
|
全体的に綺麗なコードだと思いまhした。 |
|
良いと思いました! |
|
拝見しました。良いと思います。 |
oda
reviewed
Sep 27, 2024
|
|
||
| ## Step 3 | ||
|
|
||
| 面接では速度が求められるので、比較的シンプルかつ破壊的操作のない再帰処理を用いた実装を選びました。 |
There was a problem hiding this comment.
ここは「使う人の気持ちになって私はこうするのが一番いいと思ったのでこうしたのだ、もしかしたらもっといい方法があるのかもしれないが、少なくとも私はそう判断したのだ」と言えるならばいいと思います。
小学校の低学年の頃に、母に「きれいな字を書けるかは能力であるから人による。しかし、誠実な字を書くことは誰にでもできる。誠実な字とは読み手に伝えようという意思を持った字である。」といわれたことを思い出します。
えーっと、もうちょっと生々しく、どのように、この mergeTrees が使われるか想像しませんか。
この木構造は、何かを表していて mergeTrees 以外の破壊的な関数が存在しないならば、木の一部を共有してもいいわけですが、そうでないならば、このライブラリーを使う人にびっくりさせると思います。
それで、どんなユースケースでこの mergeTrees は使われるんでしょうか。
nodchip
reviewed
Sep 28, 2024
| TreeNode[] treeNodes = treeNodeStack.pop(); | ||
| TreeNode node1 = treeNodes[0], node2 = treeNodes[1], mergedNode = treeNodes[2]; | ||
|
|
||
| if (node1.left != null || node2.left != null) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
https://leetcode.com/problems/merge-two-binary-trees/description/