98. Validate Binary Search Tree - #27
Conversation
| if node == nil { | ||
| return true | ||
| } | ||
| if node.Val <= lower || upper <= node.Val { |
There was a problem hiding this comment.
数直線上に一直線に並ぶように書くと、読み手にとって分かりやすくなると思います。
if !(lower < node.Val && node.Val < upper) {
return false
}| toVisit := []nodeWithRange{{node: root, lower: math.MinInt64, upper: math.MaxInt64}} | ||
|
|
||
| for len(toVisit) > 0 { | ||
| v := toVisit[len(toVisit)-1] |
There was a problem hiding this comment.
自分なら命名として v は current_value とかにします。
There was a problem hiding this comment.
私は、個人的には current_value は嫌ですね。
下の行と合わせて、「toVisit の一番最後を取り出す」と読めます。
それが「現在の値」であったところでほぼ情報がないのです。
There was a problem hiding this comment.
おふたりともありがとうございます
current_valueであまり情報が増えないというのは自分もそう思っています.
(とはいえvはもうちょい短命にしておくべきだったかもしれません)
| - 難しく感じてしまうのは,まだ理解が弱いんだろうなと思う.一度理解してしまえば,めちゃくちゃ読みやすくなるので. | ||
| - 快楽が大きいわかる.俺も1時間くらいかけてしまった | ||
| - https://github.com/jjysogfy/arai60-202603/pull/13 | ||
| - in-order traversalがあまりわかった気がしていない... |
There was a problem hiding this comment.
定義を確認すると良さそうですかね。
binary search tree:
https://www.geeksforgeeks.org/dsa/binary-search-tree-data-structure/
https://en.wikipedia.org/wiki/Binary_search_tree
inorder traversal:
https://www.geeksforgeeks.org/dsa/inorder-traversal-of-binary-tree/
https://en.wikipedia.org/wiki/Tree_traversal
これらの定義から、binary search treeを inorder traversalするとnode.valが昇順に列挙されることが分かります。
There was a problem hiding this comment.
ありがとうございます
geeksforgeeksというサイト,他の方のPRで見たことある気がするんですが活用できてませんでしたね.
今後参照先のひとつとして覚えておきます
https://leetcode.com/problems/validate-binary-search-tree/