Skip to content

98. Validate Binary Search Tree#30

Open
xbam326 wants to merge 3 commits into
mainfrom
98
Open

98. Validate Binary Search Tree#30
xbam326 wants to merge 3 commits into
mainfrom
98

Conversation

@xbam326
Copy link
Copy Markdown
Owner

@xbam326 xbam326 commented Feb 19, 2026

Copy link
Copy Markdown

@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.

良いと思います!メモやコードから意図が読み取りやすかったように感じます。

if node is None:
continue

valid_bst_node = more_than < node.val < less_than
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

valid_bst_node だけ見ると TreeNode のように聞こえるので、is_valid_bst_node とした方が bool っぽさがでて良いかもですね。
The Art of Readable Code になんかそんな記述があったなと思って見返してみたのですが

In general, adding words like is, has, can, or should can make booleans more clear.

とありました。この練習会でも真偽値の変数名にこれらを使用される方を多くみますね。

if root is None:
return True

more_than = -float("inf")
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

min/max や low/high だと境界を含むように聞こえるから避けられたのだと予想しますが、more_than/less_than は少し動詞っぽい響きで、特定の値を示す時にはあまり使用されないように思います。
かといってここでバッチリ当てはまる変数も思いつかないので、難しいところですね 🤔

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

low/highとかlower/upperに _exclusive を付ける、のが正確性の面では良いと思いました(ちょっと長くなりますが)

return False

def helper(node: TreeNode, more_than: int, less_than: int):
if node.left is None and node.right is None:
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

step 2 で考察されていましたが、helperやqueueにNoneを積むのを許容して、またrootNoneの時Trueを返してしまえば、これらの場合分けは要らなくなりそうですね。


def helper(node: TreeNode, more_than: int, less_than: int):
if node.left is None and node.right is None:
return more_than < node.val < less_than
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://github.com/xbam326/leetcode/pull/30/changes#r2829373715 のパターンがスッキリしそうですが、このままいくなら、何度も出てくる条件 more_than < node.val < less_than は変数に置くと分かり良いと思いました。

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.

3 participants