-
Notifications
You must be signed in to change notification settings - Fork 0
98. Validate Binary Search Tree #27
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| ### step1 | ||
|
|
||
| dfsでmin, maxを更新しながら左の葉の方がmaxと同じか大きいか、右の葉の方がminと同じか小さいかでfalseを返すように実装。 | ||
|
|
||
| ### step2 | ||
|
|
||
| 変更点なし。 | ||
|
|
||
| ### step3 | ||
|
|
||
| 3回通すまで書き直し。 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| /** | ||
| * Definition for a binary tree node. | ||
| * struct TreeNode { | ||
| * int val; | ||
| * TreeNode *left; | ||
| * TreeNode *right; | ||
| * TreeNode() : val(0), left(nullptr), right(nullptr) {} | ||
| * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} | ||
| * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} | ||
| * }; | ||
| */ | ||
| class Solution { | ||
| public: | ||
| bool traverse(TreeNode* root, long long min, long long max) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 個人的には long long の代わりに int64_t を使うことが多いです。 long long は 64 ビット以上であることが保証されているのに対し、 int64_t は 64 ビットであることが保証されているようです。
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. そうなんですね、ご指摘ありがとうございます。たしかに64ビットであることが保証されている方が安心して使えそうな気がします。 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. root は、木の根ノードを表す用語です。探索中のノードを表す変数名に付けるには違和感を感じます。 node はいかがでしょうか?
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. そうですね、nodeの方がよさそうです。 |
||
| if (!root) { | ||
| return true; | ||
| } | ||
| if (root->val >= max || root->val <= min) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if (!(min < root->val && root->val < max)) {と、数直線上に一直線になるように書くと、読み手にとって理解しやすくなると思います。
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. そうですね、ご指摘ありがとうございます。 |
||
| return false; | ||
| } | ||
| return traverse(root->left, min, root->val) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 改行する際は論理演算子を行末に書く場合もあります。 https://google.github.io/styleguide/cppguide.html#Boolean_Expressions
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 承知しました。 |
||
| && traverse(root->right, root->val, max); | ||
| } | ||
|
|
||
| bool isValidBST(TreeNode* root) { | ||
| return traverse(root, LLONG_MIN, LLONG_MAX); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. LLONG_MIN/LLONG_MAX は C 言語風に感じます。 C++11 以降では std::numerical_limits<int64_t>::min()/max() を使用することをお勧めいたします。
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ご指摘ありがとうございます。そちらを使うようにします。 |
||
| } | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| /** | ||
| * Definition for a binary tree node. | ||
| * struct TreeNode { | ||
| * int val; | ||
| * TreeNode *left; | ||
| * TreeNode *right; | ||
| * TreeNode() : val(0), left(nullptr), right(nullptr) {} | ||
| * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} | ||
| * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} | ||
| * }; | ||
| */ | ||
| class Solution { | ||
| public: | ||
| bool traverse(TreeNode* root, long long min, long long max) { | ||
| if (!root) { | ||
| return true; | ||
| } | ||
| if (root->val >= max || root->val <= min) { | ||
| return false; | ||
| } | ||
| return traverse(root->left, min, root->val) | ||
| && traverse(root->right, root->val, max); | ||
| } | ||
|
|
||
| bool isValidBST(TreeNode* root) { | ||
| return traverse(root, LLONG_MIN, LLONG_MAX); | ||
| } | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| /** | ||
| * Definition for a binary tree node. | ||
| * struct TreeNode { | ||
| * int val; | ||
| * TreeNode *left; | ||
| * TreeNode *right; | ||
| * TreeNode() : val(0), left(nullptr), right(nullptr) {} | ||
| * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} | ||
| * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} | ||
| * }; | ||
| */ | ||
| class Solution { | ||
| public: | ||
| bool traverse(TreeNode* root, long long min, long long max) { | ||
| if (!root) { | ||
| return true; | ||
| } | ||
| if (root->val >= max || root->val <= min) { | ||
| return false; | ||
| } | ||
| return traverse(root->left, min, root->val) | ||
| && traverse(root->right, root->val, max); | ||
| } | ||
|
|
||
| bool isValidBST(TreeNode* root) { | ||
| return traverse(root, LLONG_MIN, LLONG_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.
関数名は UpperCamel で書くことが多いかもしれません。 LeetCode で指定されているものについては、指定された通りに書かざるを得ないのですが…。
参考までにスタイルガイドへのリンクを共有いたします。
https://google.github.io/styleguide/cppguide.html#Function_Names
なお、このスタイルガイドは“唯一の正解”というわけではなく、数あるガイドラインの一つに過ぎません。チームによって重視される書き方や慣習も異なります。そのため、ご自身の中に基準を持ちつつも、最終的にはチームの一般的な書き方に合わせることをお勧めします。
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.
そうですね、ご指摘ありがとうございます。LeetCodeの書き方に釣られてLower camel caseで書いてしまっていました。