Skip to content

617. Merge Two Binary Trees#22

Open
seal-azarashi wants to merge 3 commits intomainfrom
merge-two-binary-trees
Open

617. Merge Two Binary Trees#22
seal-azarashi wants to merge 3 commits intomainfrom
merge-two-binary-trees

Conversation

@seal-azarashi
Copy link
Owner

}
```

これで破壊的な動作はなくなりましたが、どちらも root1, root2 をそのまま返す可能性があるのが気がかりです。問題文には "You need to merge the two trees into a new binary tree" とあるので、これを満たさないと判断されるようでしたらやはり deep copy を実施する関数を自前で実装するなり、そういった関数がある前提で処理を書くことになるのかなと考えました。

Choose a reason for hiding this comment

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

deep copyをする関数、そんなに書くの大変じゃないと思うので書いてみても良い気がしました

Choose a reason for hiding this comment

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

こんな感じでどうでしょうか。

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;
    }
};

@Yoshiki-Iwasa
Copy link

良さそうです!

@Ryotaro25
Copy link

全体的に綺麗なコードだと思いまhした。

@nittoco
Copy link

nittoco commented Sep 22, 2024

良いと思いました!

@TORUS0818
Copy link

拝見しました。良いと思います。


## Step 3

面接では速度が求められるので、比較的シンプルかつ破壊的操作のない再帰処理を用いた実装を選びました。
Copy link

Choose a reason for hiding this comment

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

ここは「使う人の気持ちになって私はこうするのが一番いいと思ったのでこうしたのだ、もしかしたらもっといい方法があるのかもしれないが、少なくとも私はそう判断したのだ」と言えるならばいいと思います。
小学校の低学年の頃に、母に「きれいな字を書けるかは能力であるから人による。しかし、誠実な字を書くことは誰にでもできる。誠実な字とは読み手に伝えようという意思を持った字である。」といわれたことを思い出します。

えーっと、もうちょっと生々しく、どのように、この mergeTrees が使われるか想像しませんか。
この木構造は、何かを表していて mergeTrees 以外の破壊的な関数が存在しないならば、木の一部を共有してもいいわけですが、そうでないならば、このライブラリーを使う人にびっくりさせると思います。

それで、どんなユースケースでこの mergeTrees は使われるんでしょうか。

TreeNode[] treeNodes = treeNodeStack.pop();
TreeNode node1 = treeNodes[0], node2 = treeNodes[1], mergedNode = treeNodes[2];

if (node1.left != null || node2.left != null) {
Copy link

Choose a reason for hiding this comment

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

left と right で同じ処理が重複しているのが気になりました。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants