-
Notifications
You must be signed in to change notification settings - Fork 0
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
98. Validate Binary Search Tree #42
base: main
Are you sure you want to change the base?
Conversation
Space: O(N) | ||
最初、部分木の最大値/最小値の算出とBST判定を同時にやろうとしてハマりかけたのでそれぞれの判定を関数に切り出してシンプルにした | ||
同時にいろんなことをしようとすると、書くのも読むのも大変なコードになりそう、というかそういうコードを考えること自体が負荷だった |
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.
} | ||
for (int i = 0; i < sorted_vals.size() - 1; ++i) { | ||
if (sorted_vals[i] >= sorted_vals[i+1]) { | ||
return false; |
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.
この判定、push_back のところでできませんか。
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.
下でしてましたね。
return true; | ||
} | ||
private: | ||
int max_val_in_tree(TreeNode* root) { |
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.
自分は関数名は UpperCamel で書くことが多いです。
https://google.github.io/styleguide/cppguide.html#Function_Names
Regular functions have mixed case; accessors and mutators may be named like variables.
Ordinarily, functions should start with a capital letter and have a capital letter for each new word.
if (!(left_val < node->val && node->val < right_val)) { | ||
return false; | ||
} | ||
return is_valid_bst_recursive(node->left, left_val, node->val) && is_valid_bst_recursive(node->right, node->val, right_val); |
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.
1 行がやや長いように感じました。定義改行を入れるとよいと思います。
Time: O(N) | ||
Space: O(N) | ||
in-order走査のメモリ改良版 |
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.
inorderの解法は再帰関数で書いてましたが、スタックでも解けるんですね。勉強になりました。
class Solution { | ||
public: | ||
bool isValidBST(TreeNode* root) { | ||
return is_valid_bst_recursive(root, numeric_limits<int64_t>::min(), numeric_limits<int64_t>::max()); |
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.
-2^31 <= Node.val <= 2^31 - 1 の制約を受けて int64_t型を使っているかと思うのですが、もう一つのmin/max対策として、*intのようなポインタ型を使ってnull判定をする方法もあります。
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.
ポインタ型を使ってnull判定をする方法もあります。
コメントありがとうございます。
それって具体的にどのようなコードになるのでしょうか?
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.
Go言語のコードですが、こんな感じです。
func isValidBST(root *TreeNode) bool {
return isValidBSTRecursively(root, nil, nil)
}
func isValidBSTRecursively(node *TreeNode, lowerBound, upperBound *int) bool {
if node == nil {
return true
}
if lowerBound != nil && node.Val <= *lowerBound {
return false
}
if upperBound != nil && node.Val >= *upperBound {
return false
}
return isValidBSTRecursively(node.Left, lowerBound, &node.Val) && isValidBSTRecursively(node.Right, &node.Val, upperBound)
}
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.
共有ありがとうございます。
個人的な感覚ですが、nullチェックを毎回行うのが冗長なので下限上限のみを使う方がしっくり来ると感じます。
https://leetcode.com/problems/validate-binary-search-tree/description/
https://discord.com/channels/1084280443945353267/1200089668901937312/1201210890142351401
hayashi-ay/leetcode#38
shining-ai/leetcode#28
YukiMichishita/LeetCode#8
Mike0121/LeetCode#8
sakupan102/arai60-practice#29
fhiyo/leetcode#30
kazukiii/leetcode#29
Yoshiki-Iwasa/Arai60#32
TORUS0818/leetcode#30
Ryotaro25/leetcode_first60#30
nittoco/leetcode#35
goto-untrapped/Arai60#52
seal-azarashi/leetcode#27
hroc135/leetcode#27
SuperHotDogCat/coding-interview#41
olsen-blue/Arai60#28