Skip to content

98 validate binary search tree - #28

Open
MA-yo-TA wants to merge 2 commits into
mainfrom
98-Validate-Binary-Search-Tree
Open

98 validate binary search tree#28
MA-yo-TA wants to merge 2 commits into
mainfrom
98-Validate-Binary-Search-Tree

Conversation

@MA-yo-TA

Copy link
Copy Markdown
Owner

if node.left is None and node.right is None:
return True, node.val, node.val

is_left_valid, left_min, left_max = _is_valid_bst_sub(node.left)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

is_left_validがfalseな時点でearly returnもできますね.

ただ,現状のコードもこれはこれで綺麗なコードだと思います.

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.

ありがとうございます。そうすることで再起呼び出し回数を減らせますね。

left_valid, left_min, left_max = results[node.left]
right_valid, right_min, right_max = results[node.right]

results[node] = (

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

left_max < node.val < right_min を満たさない場合、このタイミングで return False したほうがシンプルになると思いました。

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.

3 participants