-
Notifications
You must be signed in to change notification settings - Fork 0
Create validate-binary-search-tree.md #18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| 1回目。Gemini に聞いた | ||
|
|
||
| ```py | ||
| class Solution: | ||
| def isValidBST(self, root: Optional[TreeNode]) -> bool: | ||
| def validate(node: Optional[TreeNode], lower_bound: float, upper_bound: float) -> bool: | ||
| if node is None: | ||
| return True | ||
| if not (lower_bound < node.val < upper_bound): | ||
| return False | ||
|
|
||
| is_left_valid = validate(node.left, lower_bound, node.val) | ||
| is_right_valid = validate(node.right, node.val, upper_bound) | ||
| return is_left_valid and is_right_valid | ||
| return validate(root, float("-inf"), float("inf")) | ||
| ``` | ||
|
|
||
| 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. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. BSTをin-order traversalすると昇順に並んだ数列が得られることを利用する問題はたまに見かけるような気がしますね。具体的にどこで出会ったかは思い出せませんが... |
||
| - 以下は Gemini による回答 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. これを見て何を思ったかや、再現できるように書き直したコードを書いていただけるとコメントしやすいと思います。 |
||
| ```py | ||
| class Solution: | ||
| def isValidBST(self, root: TreeNode) -> bool: | ||
| # 直前に訪れたノードの値を保持する。 | ||
| # 初期値はマイナス無限大にしておく。 | ||
| # リストを使っているのは、再帰呼び出し間で値を共有・変更可能にするため。 | ||
| self.prev_val = float('-inf') | ||
|
|
||
| def inorder_traversal(node): | ||
| if not node: | ||
| return True | ||
|
|
||
| # 1. 左の部分木を探索 | ||
| if not inorder_traversal(node.left): | ||
| return False | ||
|
|
||
| # 2. 現在のノードを処理 | ||
| # 現在のノードの値が直前の値以下なら、BSTではない | ||
| if node.val <= self.prev_val: | ||
| return False | ||
| # 直前の値を現在のノードの値で更新 | ||
| self.prev_val = node.val | ||
|
|
||
| # 3. 右の部分木を探索 | ||
| if not inorder_traversal(node.right): | ||
| return False | ||
|
|
||
| return True | ||
|
|
||
| return inorder_traversal(root) | ||
| ``` | ||
| - 他人のコードを読むのつらいなあ... | ||
|
|
||
| 3回目。1回目のまま終了。 | ||
There was a problem hiding this comment.
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)とすると、短絡評価で速くなる場合があると思います。