Skip to content

98. Validate Binary Search Tree#27

Open
seal-azarashi wants to merge 6 commits intomainfrom
validate-binary-search-tree
Open

98. Validate Binary Search Tree#27
seal-azarashi wants to merge 6 commits intomainfrom
validate-binary-search-tree

Conversation

@seal-azarashi
Copy link
Copy Markdown
Owner

Comment on lines +26 to +29
long min = nodeMinMax.min, max = nodeMinMax.max;
if (node == null) {
continue;
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

ちょう細かいですが、if (node == null)long min = nodeMinMax.min, max = nodeMinMax.max;の前に書いても良いかなと思いました

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.

たしかにそうですね。なんでそうしなかったんだろうって思ったので修正してしまいました。
b9106c6

@Yoshiki-Iwasa
Copy link
Copy Markdown

良いと思います!

@goto-untrapped
Copy link
Copy Markdown

きれいですね!参考になりました。


次のようなことを考えながら実装していました:

- Balanced binary tree とは限らないので、再帰関数として実装すると最大で 10^4 要素がスタックに入る可能性があり、スタックオーバーフローの懸念があると判断
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

問題の制約を考えると選択肢から外れるのかもしれませんが、inorderの再帰版とか書いておくと良いのかもしれません(trivialと感じるなら余計かもですが)

Comment on lines +30 to +31
boolean isValid = min < node.val && node.val < max;
if (!isValid) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

ここでisValidを定義しないで直接でもいい気がしました。
好みの話なんでしょうけど。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

そういや、Java ではよく final 使ったりします? 変更しない変数に。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

あくまで個人の体験談ですが、クラスのフィールドのうち、コンストラクターが呼び出されたあとに変更しないものについては final にしていました。それ以外は final にしないことが多かったです。チームによって方針が違うのではないでしょうか。

Copy link
Copy Markdown
Owner Author

@seal-azarashi seal-azarashi Oct 2, 2024

Choose a reason for hiding this comment

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

nodchip さんと同じく、メンバー変数を除いて final 修飾子が付与されているのは稀でした。
しかしこうやってコメントされると、他の言語のそれと比べて何だか不思議な慣習に思えますね。自分も Go を書いているときはいつでも宣言時に変数にするか定数にするか考えるのに、 Java を扱っている時はそうではないなと気づきました。

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.

Java だとオブジェクト指向のベストプラクティスの導入のため柔軟性が重要で、イミュータビリティを与えるのに慎重になりがちなのがこのような慣習に繋がっているのかな、と思いつきましたがどうなんでしょうね。
あと単純に final + 型名 を書くのが面倒くさいというのもありそうです。元々コードの記述量が多くなりがちな言語ですから、可読性担保のために削れるものは削っておきたいみたいな考えもありそうです。

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.

6 participants