diff --git a/98/memo.md b/98/memo.md new file mode 100644 index 0000000..ebdb82c --- /dev/null +++ b/98/memo.md @@ -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 diff --git a/98/sol1_dfs_recursion.py b/98/sol1_dfs_recursion.py new file mode 100644 index 0000000..04c7bf3 --- /dev/null +++ b/98/sol1_dfs_recursion.py @@ -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")) diff --git a/98/sol2_dfs_stack.py b/98/sol2_dfs_stack.py new file mode 100644 index 0000000..2ae9f3c --- /dev/null +++ b/98/sol2_dfs_stack.py @@ -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 diff --git a/98/sol3_inorder.py b/98/sol3_inorder.py new file mode 100644 index 0000000..a7b924d --- /dev/null +++ b/98/sol3_inorder.py @@ -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