-
Notifications
You must be signed in to change notification settings - Fork 0
98. Validate Binary Search Tree #39
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
Open
skypenguins
wants to merge
1
commit into
main
Choose a base branch
from
leetcode/arai60/problem-98
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,130 @@ | ||
| # 98. Validate Binary Search Tree | ||
| - 問題: https://leetcode.com/problems/validate-binary-search-tree/ | ||
| - 言語: Python | ||
|
|
||
| ## Step1 | ||
| ### 方針 | ||
| - 与えられた二分探索木(BST)を検証するタスク | ||
| - 入力長の最大は $10^4$ で、完全二分木をDFS $O(\log n)$ で探索するから、実行時間は数ms | ||
| - DFSで、現在の節の値 <= 左節の値、もしくは現在の節の値 >= 右節の値であればFalseを返す | ||
| - `[5,4,6,null,null,3,7]` のように、数ホップ離れた右側の節の値が小さい場合にTrueと誤判定する | ||
| - 20分経過したため正答を見る | ||
|
|
||
| ### WA | ||
| ```py | ||
| class Solution: | ||
| def isValidBST(self, root: Optional[TreeNode]) -> bool: | ||
| max_val = float("-inf") | ||
| visited = [(root)] | ||
|
|
||
| while visited: | ||
| current = visited.pop() | ||
| max_val = max(max_val, current.val) | ||
|
|
||
| if current.left: | ||
| if current.val <= current.left.val: | ||
| return False | ||
| visited.append(current.left) | ||
|
|
||
| if current.right: | ||
| if current.val >= current.right.val: | ||
| return False | ||
| visited.append(current.right) | ||
|
|
||
| return True | ||
| ``` | ||
| - `current.left < current < current.right` の親子関係だけを見ているため、BST の条件を完全には判定できない | ||
|
|
||
| ### 正答 | ||
| #### スタックDFS版 | ||
| ```py | ||
| class Solution: | ||
| def isValidBST(self, root: Optional[TreeNode]) -> bool: | ||
| stack = [(root, float("-inf"), float("inf"))] | ||
|
|
||
| while stack: | ||
| node, lower, upper = stack.pop() | ||
|
|
||
| if node is None: | ||
| continue | ||
|
|
||
| if not (lower < node.val < upper): | ||
| return False | ||
|
|
||
| stack.append((node.left, lower, node.val)) | ||
| stack.append((node.right, node.val, upper)) | ||
|
|
||
| return True | ||
| ``` | ||
|
|
||
| #### 再帰DFS版 | ||
| ```py | ||
| class Solution: | ||
| def isValidBST(self, root: Optional[TreeNode]) -> bool: | ||
| def is_valid_nodes(node, lower, upper): | ||
| if node is None: | ||
| return True | ||
|
|
||
| if not (lower < node.val < upper): | ||
| return False | ||
|
|
||
| return is_valid_nodes(node.left, lower, node.val) \ | ||
| and is_valid_nodes(node.right, node.val, upper) | ||
|
|
||
| return is_valid_nodes(root, float("-inf"), float("inf")) | ||
| ``` | ||
| - 現在の節から左部分木に進むと、現在の値は上限値になる | ||
| - 現在の節から右部分木に進むと、現在の値は下限値になる | ||
| - 定義から考えたら確かに自明だなと思った | ||
|
|
||
| ## Step2 | ||
| - 典型コメント集: https://docs.google.com/document/d/11HV35ADPo9QxJOpJQ24FcZvtvioli770WWdZZDaLOfg/edit?tab=t.0#heading=h.r5pdnx6retf4 | ||
|
|
||
| - https://github.com/YukiMichishita/LeetCode/pull/8 | ||
| - Python | ||
| - return時にandで繋げて評価するのではなく、右部分木の方から見ていくパターン | ||
| - BFS、中順走査 in-order traversal(再帰、iterative)、ジェネレーターを使った方法 | ||
| - 中順走査は方針はシンプルだがコード量が多い気がする | ||
|
|
||
| | 方針 | 見ているもの | | ||
| | -------- | ------------------- | | ||
| | 範囲制約DFS | 各ノードが祖先から決まる範囲内にあるか | | ||
| | 中順走査 | ノードの値が昇順に並ぶか | | ||
|
|
||
| - https://discord.com/channels/1084280443945353267/1192736784354918470/1235116690661179465 | ||
| - https://github.com/nittoco/leetcode/pull/35 | ||
| - Python | ||
| - ジェネレーター+再帰を使うとループで回せるのでin-order sortでも割と読みやすいと思った | ||
| - > Generator + 再帰、強力ですね。pros cons の cons としては、再帰の深さの限界があること、走りかけの Generator を木の深さ分だけ作るのでそこそこ重いことがありそうですね。 | ||
| - src: https://github.com/nittoco/leetcode/pull/35/changes#r1739978684 | ||
|
|
||
| - https://github.com/SuperHotDogCat/coding-interview/pull/41 | ||
| - https://github.com/naoto-iwase/leetcode/pull/33#discussion_r2479195403 | ||
| - Python | ||
| - 葉に潜ってから走査(後順走査 post-order) | ||
|
|
||
| ## Step3 | ||
| ### 読みやすく書き直したコード | ||
| ```py | ||
| class Solution: | ||
| def isValidBST(self, root: Optional[TreeNode]) -> bool: | ||
| pending_nodes = [(root, -float("inf"), float("inf"))] | ||
|
|
||
| while pending_nodes: | ||
| node, lower_bound, upper_bound = pending_nodes.pop() | ||
|
|
||
| if node is None: | ||
| continue | ||
|
|
||
| if not (lower_bound < node.val < upper_bound): | ||
| return False | ||
|
|
||
| pending_nodes.append((node.left, lower_bound, node.val)) | ||
| pending_nodes.append((node.right, node.val, upper_bound)) | ||
|
|
||
| return True | ||
| ``` | ||
| - 所要時間: | ||
| - 1回目: 2:46 | ||
| - 2回目: 2:54 | ||
| - 3回目: 2:34 | ||
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.
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.
見やすくていいと思いました。
ただ個人的には、空行をもう少し減らしたほうが好みです。
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.
自分はこれくらいの空行が丁度良く感じます。好みの問題だと思います。
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.
そういうものなんですね。ありがとうございます。