Open
Conversation
Owner
Author
|
infはマジックナンバー感があるとの意見をみた。適当な変数に代入したほうがよさそう |
Owner
Author
|
generator使うとプロっぽくてかっこいい |
|
私は、わりと inf でいいかなと思っています。(これ自体はどちらにしてもあんまり大きな話ではないです。) |
huyfififi
reviewed
Jul 24, 2025
|
|
||
| 2回目。ほかの人のコードを見る。 | ||
| - これはとても勉強になる:https://github.com/quinn-sasha/leetcode/pull/27/files#r2200377284 | ||
| - in-order に探索する方法を知った:https://discord.com/channels/1084280443945353267/1192736784354918470/1234120299008491581 |
There was a problem hiding this comment.
BSTをin-order traversalすると昇順に並んだ数列が得られることを利用する問題はたまに見かけるような気がしますね。具体的にどこで出会ったかは思い出せませんが...
nodchip
reviewed
Jul 24, 2025
| if not (lower_bound < node.val < upper_bound): | ||
| return False | ||
|
|
||
| is_left_valid = validate(node.left, lower_bound, node.val) |
There was a problem hiding this comment.
return validate(node.left, lower_bound, node.val) and validate(node.right, node.val, upper_bound) とすると、短絡評価で速くなる場合があると思います。
tokuhirat
reviewed
Jul 24, 2025
| 2回目。ほかの人のコードを見る。 | ||
| - これはとても勉強になる:https://github.com/quinn-sasha/leetcode/pull/27/files#r2200377284 | ||
| - in-order に探索する方法を知った:https://discord.com/channels/1084280443945353267/1192736784354918470/1234120299008491581 | ||
| - 以下は Gemini による回答 |
There was a problem hiding this comment.
これを見て何を思ったかや、再現できるように書き直したコードを書いていただけるとコメントしやすいと思います。
L27 にリストとありますが正しいでしょうか?
self.prev_val というようなインスタンス変数を使わない方法として、単に prev_val = float('-inf') として関数内関数で nonlocal prev_val と書くこともできますね。こうすることで isValidBST がスレッドセーフになると思います。
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/validate-binary-search-tree/