Skip to content

Create validate-binary-search-tree.md#18

Open
tshimosake wants to merge 2 commits intomasterfrom
tshimosake-patch-16
Open

Create validate-binary-search-tree.md#18
tshimosake wants to merge 2 commits intomasterfrom
tshimosake-patch-16

Conversation

@tshimosake
Copy link
Owner

@tshimosake
Copy link
Owner Author

infはマジックナンバー感があるとの意見をみた。適当な変数に代入したほうがよさそう

@tshimosake
Copy link
Owner Author

generator使うとプロっぽくてかっこいい

nittoco/leetcode#35 (comment)

@oda
Copy link

oda commented Jul 18, 2025

私は、わりと inf でいいかなと思っています。(これ自体はどちらにしてもあんまり大きな話ではないです。)
考えるときに、inf にすることによって将来困ることがあるかがポイントですが、これはどちらでもあまりないだろうなと思います。

@tshimosake tshimosake marked this pull request as ready for review July 23, 2025 09:57
Copy link

@huyfififi huyfififi left a comment

Choose a reason for hiding this comment

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

いいと思います 👍


2回目。ほかの人のコードを見る。
- これはとても勉強になる:https://github.com/quinn-sasha/leetcode/pull/27/files#r2200377284
- in-order に探索する方法を知った:https://discord.com/channels/1084280443945353267/1192736784354918470/1234120299008491581

Choose a reason for hiding this comment

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

BSTをin-order traversalすると昇順に並んだ数列が得られることを利用する問題はたまに見かけるような気がしますね。具体的にどこで出会ったかは思い出せませんが...

if not (lower_bound < node.val < upper_bound):
return False

is_left_valid = validate(node.left, lower_bound, node.val)
Copy link

Choose a reason for hiding this comment

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

return validate(node.left, lower_bound, node.val) and validate(node.right, node.val, upper_bound) とすると、短絡評価で速くなる場合があると思います。

2回目。ほかの人のコードを見る。
- これはとても勉強になる:https://github.com/quinn-sasha/leetcode/pull/27/files#r2200377284
- in-order に探索する方法を知った:https://discord.com/channels/1084280443945353267/1192736784354918470/1234120299008491581
- 以下は Gemini による回答

Choose a reason for hiding this comment

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

これを見て何を思ったかや、再現できるように書き直したコードを書いていただけるとコメントしやすいと思います。
L27 にリストとありますが正しいでしょうか?
self.prev_val というようなインスタンス変数を使わない方法として、単に prev_val = float('-inf') として関数内関数で nonlocal prev_val と書くこともできますね。こうすることで isValidBST がスレッドセーフになると思います。

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