Skip to content

Create 617. Merge Two Binary Trees#31

Open
Apo-Matchbox wants to merge 1 commit intomainfrom
617.-Merge-Two-Binary-Trees
Open

Create 617. Merge Two Binary Trees#31
Apo-Matchbox wants to merge 1 commit intomainfrom
617.-Merge-Two-Binary-Trees

Conversation

@Apo-Matchbox
Copy link
Copy Markdown
Owner


```

## Step2
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

step2とStep3のコードが無い気がします。アップいただけますでしょうか。

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.

@Ryotaro25

今回の問題に関してはstep1が自然に感じるのでそのままstep3へ
という形を取りましたが、他の方からのご指摘あります通りroot1に合わせるような破壊的なマージ以外のものに関しても今後書いていきたいと思います。

- 新しい二分木:new TreeNode()を作成する。root1/root2を壊さずに作成
- node1, node2, merged_nodeと3つの組み合わせをキューに入れる
- dummyを用意
- 関数内でメモリを確保した場合、呼び出し元で解放しないとメモリリークとなる。ので面倒で選択してない。
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

リンク先のコメントが参考になりました。C++の経験がないといつもの癖でnewとしそうなので、今後C++を書く際に気をつけられそうです。

Comment on lines +73 to +79
- 他の解法
- python
- https://github.com/tshimosake/arai60/pull/14/commits/253f33c504c73c4709dafa575c47a9a0f3f0b5ab
-
- C++
- https://github.com/irohafternoon/LeetCode/pull/26/commits/d67d4b4a3959fb02d773249be55806988155817b
- https://github.com/Ryotaro25/leetcode_first60/pull/25/commits/b67018715bbcb6efaa049d09590460cbb23774b1#diff-1296319066657d7ffa73cc35ea8dab0b8bb061399fa3382e925bf62f940038e3
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

URLだけでなく読んだ感想をペアにして列挙していただくとレビュワーの助けになると思います。

例:Step Xの実装が良い、読みやすい、シンプルだと思った。ここの部分は自分ならこう書きたい。ここの部分で複数の選択肢を検討していて良いと思った。など

- 制約の確認
- The number of nodes in both trees is in the range [0, 2000].
- ノードの数は最大:2000
- O(N), O(n log n)がベター、O(n^2)だと効率という点では微妙かな
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

ここを読んで少なくとも自分の感覚とはズレていると感じました。

自分はこの問題を見たとき、rootからトップダウンに作業してO(n)で行けるのはほぼ自明で、速度の議論については定数倍でしか起きないと感じました。

それよりも重要なのは、Step 1で書かれたコードはほぼ最速ですが、入力を破壊している点が気になります。返り値のある関数で入力が破壊される場合、呼び出した人はびっくりすると思います。
実務ならAPIドキュメントに仕様として明記しておいたり、入力を破壊するかどうか引数で選択できるなどの配慮が必要だと思います。

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.

@docto-rin
ご指摘ありがとうございます。
root1 を上書きする破壊的なマージになっている点認識しました。LeetCode上では問題ないように見えますが、実務的なAPIであれば「入力を破壊すること」をドキュメントで明示するか、非破壊的を用意するのが望ましいという点大変勉強になりました。
また実務経験はないのですが、この点他の方のコードを読んだ時に気づけたはずだったのでどんな違いがあるのか注意深く意識してみます。

/*次にコードを読みやすくなるようにできるだけ整えましょう。これで動くコードになったら完成です。*/
- 改善点
- 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.

進め方は人それぞれなのでお任せしたいですが、全体的に拝見し、個人的にはもう少し選択肢とそれらのトレードオフについて広く検討されても良いかもしれないなと感じました。例えばですが:

再帰

https://docs.google.com/document/d/11HV35ADPo9QxJOpJQ24FcZvtvioli770WWdZZDaLOfg/edit?tab=t.0#heading=h.deivkzaqvetb

再帰をすると、少し面倒になるのがデバッグのためのログ関数を挟むときなどです。
なので、単純なループに近いときには再帰でわざわざ書くことはあまりないとは思います。ただ結構考えるときには使っています。

https://discord.com/channels/1084280443945353267/1350090869390311494/1354492544049877134

例えばですけれども、1万回くらい呼ぶと1回くらいおかしな動きをする機能があって、乱数などが絡んでいるから再現も難しいので、ある特定の if 文を通ったときにログを出力したいとします。再帰で書いていると、そもそもどういう状況でそこに到達したのかの関数の呼び出し元の情報などを出力するのが大変です。

https://discord.com/channels/1084280443945353267/1350090869390311494/1355224179720458261

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.

@docto-rin
アドバイスありがとうございます。
ご指摘の通りだと思いました。Arai60に取り組んだ初めの方、選択肢をどんどん増やそうと出てきたものを全部調べてると時間をかなり使ってしまい結果的に前に進まないという事になりました。これは避けたいですが、同時に引き出しを増やす時間がすごく大事ですので折り合いをつけながら学習を進めたいと思います。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

飽きが一番の敵で、書けるようになると周りを見回す余裕ができるので将来戻ってくるのでもいいですよ。

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.

@oda
アドバイスありがとうございます。
まずは前に進めていこうと思います。

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.

5 participants