Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use leetcode to restore binary search Tree in golang

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report