How to use leetcode to restore binary search Tree in golang
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.