Skip to content

98. Validate Binary Search Tree - #27

Open
miyataka wants to merge 1 commit into
mainfrom
98_ValidateBinarySearchTree
Open

98. Validate Binary Search Tree#27
miyataka wants to merge 1 commit into
mainfrom
98_ValidateBinarySearchTree

Conversation

@miyataka

Copy link
Copy Markdown
Owner

if node == nil {
return true
}
if node.Val <= lower || upper <= node.Val {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

数直線上に一直線に並ぶように書くと、読み手にとって分かりやすくなると思います。

if !(lower < node.Val && node.Val < upper) {
    return false
}

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.

ありがとうございます

toVisit := []nodeWithRange{{node: root, lower: math.MinInt64, upper: math.MaxInt64}}

for len(toVisit) > 0 {
v := toVisit[len(toVisit)-1]

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

自分なら命名として vcurrent_value とかにします。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

私は、個人的には current_value は嫌ですね。
下の行と合わせて、「toVisit の一番最後を取り出す」と読めます。
それが「現在の値」であったところでほぼ情報がないのです。

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.

おふたりともありがとうございます

current_valueであまり情報が増えないというのは自分もそう思っています.
(とはいえvはもうちょい短命にしておくべきだったかもしれません)

- 難しく感じてしまうのは,まだ理解が弱いんだろうなと思う.一度理解してしまえば,めちゃくちゃ読みやすくなるので.
- 快楽が大きいわかる.俺も1時間くらいかけてしまった
- https://github.com/jjysogfy/arai60-202603/pull/13
- in-order traversalがあまりわかった気がしていない...

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

定義を確認すると良さそうですかね。
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が昇順に列挙されることが分かります。

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.

ありがとうございます
geeksforgeeksというサイト,他の方のPRで見たことある気がするんですが活用できてませんでしたね.
今後参照先のひとつとして覚えておきます

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants