Skip to content

Conversation

Space: O(N)

最初、部分木の最大値/最小値の算出とBST判定を同時にやろうとしてハマりかけたのでそれぞれの判定を関数に切り出してシンプルにした
同時にいろんなことをしようとすると、書くのも読むのも大変なコードになりそう、というかそういうコードを考えること自体が負荷だった
Copy link

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;
Copy link

Choose a reason for hiding this comment

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

この判定、push_back のところでできませんか。

Copy link

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) {
Copy link

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);
Copy link

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走査のメモリ改良版

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());
Copy link

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判定をする方法もあります。

Copy link
Owner Author

Choose a reason for hiding this comment

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

ポインタ型を使ってnull判定をする方法もあります。

コメントありがとうございます。
それって具体的にどのようなコードになるのでしょうか?

Copy link

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)
}

Copy link
Owner Author

Choose a reason for hiding this comment

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

共有ありがとうございます。
個人的な感覚ですが、nullチェックを毎回行うのが冗長なので下限上限のみを使う方がしっくり来ると感じます。

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.

6 participants