-
Notifications
You must be signed in to change notification settings - Fork 0
98. Validate Binary Search Tree #29
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
hiroki-horiguchi-dev
wants to merge
4
commits into
main
Choose a base branch
from
tree-bst-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
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,330 @@ | ||
| - [98. Validate Binary Search Tree](https://leetcode.com/problems/validate-binary-search-tree/description/) | ||
| - 方針 | ||
| - Queue を用いる | ||
| - 初期値の `root.val` は `Long.MIN_VALUE ~ Long.MAX_VALUE` の間に収まっていればOK | ||
| - 同様に、左の子については、最大値が root.val になればよく、左の子の値判定は、`Long.MIN_VALUE ~ root.val` に収まっていればBSTとして適切 | ||
| - 同様に、右の子については、最小値が root.val になればよく、右の子の値判定は、`root.val ~ Long.MAX_VALUE` に収まっていればBSTとして適切 | ||
| - 上記を繰り返すだけではなく、一つ下のノードに落ちた時に左に落ちたのであれば、最大値を更新。右に落ちたのであれば最小値を更新して、許容される値の区間を更新し続ける必要がある | ||
| - 一般化すると、左に行く時は最大値を現在のノードの値で更新、右に行く時は現在のノードの値で最小値を更新すれば良い | ||
| - 実装時間: 20分 | ||
| ```java | ||
| /** | ||
| * Definition for a binary tree node. | ||
| * public class TreeNode { | ||
| * int val; | ||
| * TreeNode left; | ||
| * TreeNode right; | ||
| * TreeNode() {} | ||
| * TreeNode(int val) { this.val = val; } | ||
| * TreeNode(int val, TreeNode left, TreeNode right) { | ||
| * this.val = val; | ||
| * this.left = left; | ||
| * this.right = right; | ||
| * } | ||
| * } | ||
| */ | ||
| class Solution { | ||
| public boolean isValidBST(TreeNode root) { | ||
| if (root == null) { | ||
| return false; | ||
| } | ||
|
|
||
| Deque<NodeWithRange> nodes = new ArrayDeque<>(); | ||
| nodes.addLast(new NodeWithRange(root, Long.MIN_VALUE, Long.MAX_VALUE)); | ||
|
|
||
| while (!nodes.isEmpty()) { | ||
| NodeWithRange nodeWithRange = nodes.pollFirst(); | ||
| TreeNode node = nodeWithRange.node; | ||
| long min = nodeWithRange.min; | ||
| long max = nodeWithRange.max; | ||
|
|
||
| if (node.val <= min || node.val >= max) { | ||
| return false; | ||
| } | ||
|
|
||
| if (node.left != null) { | ||
| nodes.addLast(new NodeWithRange(node.left, min, node.val)); | ||
| } | ||
|
|
||
| if (node.right != null) { | ||
| nodes.addLast(new NodeWithRange(node.right, node.val, max)); | ||
| } | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| private record NodeWithRange(TreeNode node, long min, long max){} | ||
| } | ||
| ``` | ||
| - うまくいかず、40分程度格闘したコード | ||
| - 何がいけなかったのか? | ||
| - Queue に対するノードの追加と invalid の判定を同時に行おうとした点 | ||
| ```java | ||
| /** | ||
| * Definition for a binary tree node. | ||
| * public class TreeNode { | ||
| * int val; | ||
| * TreeNode left; | ||
| * TreeNode right; | ||
| * TreeNode() {} | ||
| * TreeNode(int val) { this.val = val; } | ||
| * TreeNode(int val, TreeNode left, TreeNode right) { | ||
| * this.val = val; | ||
| * this.left = left; | ||
| * this.right = right; | ||
| * } | ||
| * } | ||
| */ | ||
| class Solution { | ||
| public boolean isValidBST(TreeNode root) { | ||
| if (root == null) { | ||
| return false; | ||
| } | ||
|
|
||
| Deque<NodeWithRange> nodes = new ArrayDeque<>(); | ||
| nodes.addLast(new NodeWithRange(root, Long.MIN_VALUE, Long.MAX_VALUE)); | ||
| while (!nodes.isEmpty()) { | ||
| NodeWithRange nodeWithRange = nodes.pollFirst(); | ||
| TreeNode node = nodeWithRange.node; | ||
| long min = nodeWithRange.min; | ||
| long max = nodeWithRange.max; | ||
|
|
||
| if (node.left != null) { | ||
| if (node.left.val >= node.val || node.left.val <= min) { | ||
| return false; | ||
| } | ||
| nodes.addLast(new NodeWithRange(node.left, min, node.val)); | ||
| } | ||
|
|
||
| if (node.right != null) { | ||
| if (node.right.val <= node.val || node.right.val >= max) { | ||
| return false; | ||
| } | ||
| nodes.addLast(new NodeWithRange(node.right, node.val, max)); | ||
| } | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| private record NodeWithRange(TreeNode node, long min, long max){} | ||
| } | ||
| ``` | ||
|
|
||
| - 時間を空けて解き直し、もっとシンプルにできる | ||
| ```java | ||
| /** | ||
| * Definition for a binary tree node. | ||
| * public class TreeNode { | ||
| * int val; | ||
| * TreeNode left; | ||
| * TreeNode right; | ||
| * TreeNode() {} | ||
| * TreeNode(int val) { this.val = val; } | ||
| * TreeNode(int val, TreeNode left, TreeNode right) { | ||
| * this.val = val; | ||
| * this.left = left; | ||
| * this.right = right; | ||
| * } | ||
| * } | ||
| */ | ||
| class Solution { | ||
| public boolean isValidBST(TreeNode root) { | ||
| if (root == null) { | ||
| return false; | ||
| } | ||
|
|
||
| Deque<NodeWithRange> nodes = new ArrayDeque<>(); | ||
| nodes.addLast(new NodeWithRange(root, Long.MIN_VALUE, Long.MAX_VALUE)); | ||
|
|
||
| while (!nodes.isEmpty()) { | ||
| NodeWithRange nodeWithRange = nodes.pollFirst(); | ||
| TreeNode node = nodeWithRange.node; | ||
| long min = nodeWithRange.min; | ||
| long max = nodeWithRange.max; | ||
|
|
||
| if (node.val <= min || node.val >= max) { | ||
| return false; | ||
| } | ||
|
|
||
| if (node.left != null) { | ||
| // 最大値更新 | ||
| nodes.addLast(new NodeWithRange(node.left, min, node.val)); | ||
| } | ||
|
|
||
| if (node.right != null) { | ||
| // 最小値更新 | ||
| nodes.addLast(new NodeWithRange(node.right, node.val, max)); | ||
| } | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| private record NodeWithRange(TreeNode node, long min, long max){} | ||
| } | ||
| ``` | ||
|
|
||
| - 前回の課題をまとめる | ||
| - [103. Binary Tree Zigzag Level Order Traversal #27 line 145](https://github.com/hiroki-horiguchi-dev/leetcode/pull/27/changes)で自分に出した課題 | ||
|
|
||
| ```terminaloutput | ||
| Iterable | ||
| └── Collection | ||
| ├── List | ||
| │ ├── ArrayList | ||
| │ └── LinkedList | ||
| ├── Set | ||
| │ ├── HashSet | ||
| │ └── TreeSet | ||
| └── Queue | ||
| ├── LinkedList (List も実装してる) | ||
| └── ArrayDeque | ||
| ``` | ||
|
|
||
| - [Iterable](https://github.com/openjdk/jdk/blob/a13dd293a811a6ba829696e68cd2150de2cb2f17/src/java.base/share/classes/java/lang/Iterable.java) | ||
| - Iterable は、Javaの Collection 自体が実装しているので、Collectionを継承ないし実装しているクラス(List, Queue, Set, Map) は全て for を使うことができる | ||
| - CSZAP の チューリング完全あたりで出てきた内容っぽいので入門コンピュータ科学12章をつまみ読んだ | ||
| ```java | ||
| public interface Iterable<T> { | ||
| Iterator<T> iterator(); | ||
|
|
||
| default void forEach(Consumer<? super T> action) { | ||
| Objects.requireNonNull(action); | ||
| for (T t : this) { | ||
| action.accept(t); | ||
| } | ||
| } | ||
| default Spliterator<T> spliterator() { | ||
| return Spliterators.spliteratorUnknownSize(iterator(), 0); | ||
| } | ||
| } | ||
| ``` | ||
| - [Collections](https://github.com/openjdk/jdk/blob/a13dd293a811a6ba829696e68cd2150de2cb2f17/src/java.base/share/classes/java/util/Collection.java) | ||
| ```java | ||
| package java.util; | ||
|
|
||
| import java.util.function.IntFunction; | ||
| import java.util.function.Predicate; | ||
| import java.util.stream.Stream; | ||
| import java.util.stream.StreamSupport; | ||
|
|
||
| public interface Collection<E> extends Iterable<E> { | ||
|
|
||
| int size(); | ||
|
|
||
| boolean isEmpty(); | ||
|
|
||
| boolean contains(Object o); | ||
|
|
||
| Iterator<E> iterator(); | ||
|
|
||
| Object[] toArray(); | ||
|
|
||
| <T> T[] toArray(T[] a); | ||
|
|
||
| default <T> T[] toArray(IntFunction<T[]> generator) { | ||
| return toArray(generator.apply(0)); | ||
| } | ||
|
|
||
| boolean add(E e); | ||
|
|
||
| boolean remove(Object o); | ||
|
|
||
| boolean containsAll(Collection<?> c); | ||
|
|
||
| boolean addAll(Collection<? extends E> c); | ||
|
|
||
| boolean removeAll(Collection<?> c); | ||
|
|
||
| default boolean removeIf(Predicate<? super E> filter) { | ||
| Objects.requireNonNull(filter); | ||
| boolean removed = false; | ||
| final Iterator<E> each = iterator(); | ||
| while (each.hasNext()) { | ||
| if (filter.test(each.next())) { | ||
| each.remove(); | ||
| removed = true; | ||
| } | ||
| } | ||
| return removed; | ||
| } | ||
|
|
||
| boolean retainAll(Collection<?> c); | ||
|
|
||
| void clear(); | ||
|
|
||
| boolean equals(Object o); | ||
|
|
||
| int hashCode(); | ||
|
|
||
| @Override | ||
| default Spliterator<E> spliterator() { | ||
| return Spliterators.spliterator(this, 0); | ||
| } | ||
|
|
||
| default Stream<E> stream() { | ||
| return StreamSupport.stream(spliterator(), false); | ||
| } | ||
|
|
||
| default Stream<E> parallelStream() { | ||
| return StreamSupport.stream(spliterator(), true); | ||
| } | ||
| } | ||
| ``` | ||
| - 抽象メソッド(実装必須) | ||
| - 情報取得: `size(), isEmpty(), contains()` | ||
| - 走査: `iterator()` | ||
| - 変換: `toArray(), toArray(T[])` | ||
| - 追加/削除: `add(), remove(), addAll(), removeAll(), retainAll(), clear()` | ||
| - 存在確認: `containsAll()` | ||
| - 比較: `equals(), hashCode()` | ||
| - 実装しているクラスのまとめ | ||
| ```terminaloutput | ||
| Collection [interface] | ||
| ├── SequencedCollection [interface] (Java 21〜) | ||
| │ ├── List [interface] | ||
| │ │ ├── ArrayList | ||
| │ │ ├── LinkedList (※Dequeも実装) | ||
| │ │ ├── Vector | ||
| │ │ │ └── Stack | ||
| │ │ └── CopyOnWriteArrayList | ||
| │ ├── SequencedSet [interface] (Java 21〜 / Setを継承) | ||
| │ │ ├── LinkedHashSet (※HashSetの子クラス) | ||
| │ │ └── TreeSet (※NavigableSet経由で継承) | ||
| │ └── Deque [interface] (※Queueも継承) | ||
| │ ├── ArrayDeque | ||
| │ ├── LinkedList (※Listも実装) | ||
| │ ├── ConcurrentLinkedDeque | ||
| │ └── BlockingDeque [interface] | ||
| │ └── LinkedBlockingDeque | ||
| │ | ||
| ├── Set [interface] | ||
| │ ├── HashSet | ||
| │ │ └── LinkedHashSet (※SequencedSetも実装) | ||
| │ ├── EnumSet | ||
| │ ├── TreeSet (※NavigableSet経由) | ||
| │ └── CopyOnWriteArraySet | ||
| │ | ||
| ├── Queue [interface] | ||
| │ ├── PriorityQueue | ||
| │ ├── ConcurrentLinkedQueue | ||
| │ ├── Deque [interface] (※上記SequencedCollectionの枠内へ移動) | ||
| │ └── BlockingQueue [interface] | ||
| │ ├── ArrayBlockingQueue | ||
| │ ├── LinkedBlockingQueue | ||
| │ ├── PriorityBlockingQueue | ||
| │ ├── SynchronousQueue | ||
| │ └── LinkedTransferQueue | ||
| │ | ||
| └── AbstractCollection [abstract] | ||
| ├── AbstractList [abstract] ──> ArrayList, Vector などの親 | ||
| │ └── AbstractSequentialList [abstract] ──> LinkedList の親 | ||
| ├── AbstractSet [abstract] ──> HashSet, TreeSet などの親 | ||
| └── AbstractQueue [abstract] ──> PriorityQueue などの親 | ||
| ``` | ||
| - Map は実装していないので注意。Map を entrySet で回せているように見えるのは Set が Iterator を実装しているからで Map が Iterator を実装しているからではない | ||
| - 面倒くさくなってきたので一旦ここで push する | ||
| - あとは気が向けば戻ってきて続きをやる | ||
|
|
||
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.
ありがとうございます。
! の使用を避けて書きましたが、A && B の否定であればわかりやすいですね。