Conversation
| if node is None: | ||
| continue | ||
|
|
||
| valid_bst_node = more_than < node.val < less_than |
There was a problem hiding this comment.
valid_bst_node だけ見ると TreeNode のように聞こえるので、is_valid_bst_node とした方が bool っぽさがでて良いかもですね。
The Art of Readable Code になんかそんな記述があったなと思って見返してみたのですが
In general, adding words like
is,has,can, orshouldcan make booleans more clear.
とありました。この練習会でも真偽値の変数名にこれらを使用される方を多くみますね。
| if root is None: | ||
| return True | ||
|
|
||
| more_than = -float("inf") |
There was a problem hiding this comment.
min/max や low/high だと境界を含むように聞こえるから避けられたのだと予想しますが、more_than/less_than は少し動詞っぽい響きで、特定の値を示す時にはあまり使用されないように思います。
かといってここでバッチリ当てはまる変数も思いつかないので、難しいところですね 🤔
There was a problem hiding this comment.
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: |
There was a problem hiding this comment.
step 2 で考察されていましたが、helperやqueueにNoneを積むのを許容して、またrootがNoneの時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 |
There was a problem hiding this comment.
https://github.com/xbam326/leetcode/pull/30/changes#r2829373715 のパターンがスッキリしそうですが、このままいくなら、何度も出てくる条件 more_than < node.val < less_than は変数に置くと分かり良いと思いました。
問題: 98. Validate Binary Search Tree
次に解く問題: 105. Construct Binary Tree from Preorder and Inorder Traversal