Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions 0098-validate-binary-search-tree/memo.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
### step1

dfsでmin, maxを更新しながら左の葉の方がmaxと同じか大きいか、右の葉の方がminと同じか小さいかでfalseを返すように実装。

### step2

変更点なし。

### step3

3回通すまで書き直し。
28 changes: 28 additions & 0 deletions 0098-validate-binary-search-tree/step1.cpp
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) {
Copy link
Copy Markdown

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

Ordinarily, functions follow PascalCase: start with a capital letter and have a capital letter for each new word.

なお、このスタイルガイドは“唯一の正解”というわけではなく、数あるガイドラインの一つに過ぎません。チームによって重視される書き方や慣習も異なります。そのため、ご自身の中に基準を持ちつつも、最終的にはチームの一般的な書き方に合わせることをお勧めします。

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.

そうですね、ご指摘ありがとうございます。LeetCodeの書き方に釣られてLower camel caseで書いてしまっていました。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

個人的には long long の代わりに int64_t を使うことが多いです。 long long は 64 ビット以上であることが保証されているのに対し、 int64_t は 64 ビットであることが保証されているようです。
https://cpprefjp.github.io/lang/cpp11/long_long_type.html

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.

そうなんですね、ご指摘ありがとうございます。たしかに64ビットであることが保証されている方が安心して使えそうな気がします。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

root は、木の根ノードを表す用語です。探索中のノードを表す変数名に付けるには違和感を感じます。 node はいかがでしょうか?

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.

そうですね、nodeの方がよさそうです。

if (!root) {
return true;
}
if (root->val >= max || root->val <= min) {
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 (!(min < root->val && root->val < max)) {

と、数直線上に一直線になるように書くと、読み手にとって理解しやすくなると思います。

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.

そうですね、ご指摘ありがとうございます。

return false;
}
return traverse(root->left, min, root->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.

改行する際は論理演算子を行末に書く場合もあります。

https://google.github.io/styleguide/cppguide.html#Boolean_Expressions

Note that when the code wraps in this example, both of the && logical AND operators are at the end of the line. This is more common in Google code, though wrapping all operators at the beginning of the line is also allowed.

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.

承知しました。

&& traverse(root->right, root->val, max);
}

bool isValidBST(TreeNode* root) {
return traverse(root, LLONG_MIN, LLONG_MAX);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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() を使用することをお勧めいたします。

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.

ご指摘ありがとうございます。そちらを使うようにします。

}
};
28 changes: 28 additions & 0 deletions 0098-validate-binary-search-tree/step2.cpp
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);
}
};
28 changes: 28 additions & 0 deletions 0098-validate-binary-search-tree/step3.cpp
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);
}
};