Skip to content

776. Split BST#49

Open
ryoooooory wants to merge 1 commit intomainfrom
task/776
Open

776. Split BST#49
ryoooooory wants to merge 1 commit intomainfrom
task/776

Conversation

@ryoooooory
Copy link
Copy Markdown
Owner

問題:https://leetcode.com/problems/split-bst/description/

プレミアムなので問題文をコピー
Given the root of a binary search tree (BST) and an integer target, split the tree into two subtrees where the first subtree has nodes that are all smaller or equal to the target value, while the second subtree has all nodes that are greater than the target value. It is not necessarily the case that the tree contains a node with the value target.

Additionally, most of the structure of the original tree should remain. Formally, for any child c with parent p in the original tree, if they are both in the same subtree after the split, then node c should still have the parent p.

Return an array of the two roots of the two subtrees in order.

Example 1:

Input: root = [4,2,6,1,3,5,7], target = 2
Output: [[2,1],[4,3,6,null,null,5,7]]

O(h): 木の深さ

・所感
簡単なテストケースのみで問題の本質を誤認してしまった。またBSTの内容を理解していれば、単純に親との関係を見るだけではダメなことは気付けだはず。
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

紙と鉛筆で絵を書く、くらいですかねえ……。

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