Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use leetcode to restore binary search Tree in golang

Shulou Source: shulou.com Published: 2022-06-02 07:44:50 10月26日 Update

Golang how to use leetcode to restore the binary search tree, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can get something.

Two nodes in the binary search tree are incorrectly swapped.

Please restore the tree without changing its structure.

Example 1:

Input: [1pyrum 3gr nullpor nullpr 2]

one

/

three

\

two

Output: [3pyrrine 1pyrmage nullpome 2]

three

/

one

\

two

Example 2:

Input: [3Jing 1pr 4pr nullpr nullPol 2]

three

/\

1 4

/

two

Output: [2pyrum 1pyrm 4pr nullpr nullpr 3]

two

/\

1 4

/

three

Advanced:

It is easy to realize by using the solution of O (n) space complexity.

Can you come up with a solution that only uses constant space?

Ideas for solving the problem:

1, the nature of the binary tree: will the left subtree cur.Val {if first==nil {first=pre second=cur} else {second=cur}} pre=cur midOrder (cur.Right)} help you after reading the above? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Tags: Location two search sequence example space subtree help input output complexity clarity global content just right complexity this constant ideas Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei vpn Docker Microsoft Apple