Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions 98/memo.md
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
23 changes: 23 additions & 0 deletions 98/sol1_dfs_recursion.py
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"))
27 changes: 27 additions & 0 deletions 98/sol2_dfs_stack.py
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
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

個人的な好みですが、whilte や for 文などの後には一つ空行を入れるようにしています。たまに目が滑って、while 文中だと勘違いしてしまうので 😓

Copy link
Copy Markdown
Owner Author

@tom4649 tom4649 Mar 25, 2026

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
にも関連しそうなことが書かれていますね。

29 changes: 29 additions & 0 deletions 98/sol3_inorder.py
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

この解法、[1,2] とかで落ちませんか?

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

修正しました。修正前のコードではテストに通りませんでした(違うコードをpushしてしまったようです)。

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