-
Notifications
You must be signed in to change notification settings - Fork 0
98. Validate Binary Search Tree #27
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
tom4649
wants to merge
4
commits into
main
Choose a base branch
from
98.Validate-Binary-Search-Tree
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
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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,18 @@ | ||
| # 98. Validate Binary Search Tree | ||
| [リンク](https://leetcode.com/problems/validate-binary-search-tree/submissions/1952536263/) | ||
|
|
||
| - 再帰 dfsで書いた: sol1_dfs_recursion.py | ||
| - 色々な解法: https://github.com/mamo3gr/arai60/blob/98_validate-binary-search-tree/98_validate-binary-search-tree/memo.md | ||
| - inorderに探索し、昇順になっているかを確認するのかでもとける | ||
| - inorder + 再帰 | ||
| - https://github.com/nittoco/leetcode/pull/35/changes/BASE..cf57a354ba6d4fd06a3454283c3cec50011ce0c4#r1739978684 | ||
| - inorder + stackで書いてみる sol3 | ||
|
|
||
| - 帰りがけ iterative | ||
| - https://github.com/naoto-iwase/leetcode/pull/33#discussion_r2479195403 | ||
| - これは自分で書けそうにない | ||
| - 親の left or rightに新しいノードが加えられる | ||
|
|
||
| - stack dfs | ||
| - https://github.com/mamo3gr/arai60/blob/98_validate-binary-search-tree/98_validate-binary-search-tree/step3.py | ||
| - 書いてみる: sol2 |
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,23 @@ | ||
| from typing import Optional | ||
|
|
||
|
|
||
| # Definition for a binary tree node. | ||
| class TreeNode: | ||
| def __init__(self, val=0, left=None, right=None): | ||
| self.val = val | ||
| self.left = left | ||
| self.right = right | ||
|
|
||
|
|
||
| class Solution: | ||
| def isValidBST(self, root: Optional[TreeNode]) -> bool: | ||
| def isValidBST_with_range(node, must_be_greater_than, must_be_less_than): | ||
| if node is None: | ||
| return True | ||
| if node.val <= must_be_greater_than or node.val >= must_be_less_than: | ||
| return False | ||
| return isValidBST_with_range( | ||
| node.left, must_be_greater_than, node.val | ||
| ) and isValidBST_with_range(node.right, node.val, must_be_less_than) | ||
|
|
||
| return isValidBST_with_range(root, -float("inf"), float("inf")) |
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,27 @@ | ||
| from typing import Optional | ||
|
|
||
|
|
||
| # Definition for a binary tree node. | ||
| class TreeNode: | ||
| def __init__(self, val=0, left=None, right=None): | ||
| self.val = val | ||
| self.left = left | ||
| self.right = right | ||
|
|
||
|
|
||
| class Solution: | ||
| def isValidBST(self, root: Optional[TreeNode]) -> bool: | ||
| if root is None: | ||
| return True | ||
|
|
||
| frontier = [(root, -float("inf"), float("inf"))] | ||
| while frontier: | ||
| node, must_be_greater_than, must_be_less_than = frontier.pop() | ||
| if not must_be_greater_than < node.val < must_be_less_than: | ||
| return False | ||
| if node.left is not None: | ||
| frontier.append((node.left, must_be_greater_than, node.val)) | ||
| if node.right is not None: | ||
| frontier.append((node.right, node.val, must_be_less_than)) | ||
|
|
||
| return True | ||
|
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. この解法、[1,2] とかで落ちませんか?
Owner
Author
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. 修正しました。修正前のコードではテストに通りませんでした(違うコードをpushしてしまったようです)。 |
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,29 @@ | ||
| from typing import Optional | ||
|
|
||
|
|
||
| # Definition for a binary tree node. | ||
| class TreeNode: | ||
| def __init__(self, val=0, left=None, right=None): | ||
| self.val = val | ||
| self.left = left | ||
| self.right = right | ||
|
|
||
|
|
||
| class Solution: | ||
| def isValidBST(self, root: Optional[TreeNode]) -> bool: | ||
| frontier = [] | ||
|
|
||
| def push_it_and_left_children(node): | ||
| while node is not None: | ||
| frontier.append(node) | ||
| node = node.left | ||
|
|
||
| push_it_and_left_children(root) | ||
| min_value = -float("inf") | ||
| while frontier: | ||
| node = frontier.pop() | ||
| if min_value >= node.val: | ||
| return False | ||
| min_value = node.val | ||
| push_it_and_left_children(node.right) | ||
| return True |
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.
個人的な好みですが、whilte や for 文などの後には一つ空行を入れるようにしています。たまに目が滑って、while 文中だと勘違いしてしまうので 😓
Uh oh!
There was an error while loading. Please reload this page.
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.
なるほど、勉強になります。他の方でもこのようにされている方は多そうですね。
https://peps.python.org/pep-0008/#blank-lines
にも関連しそうなことが書かれていますね。