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 realize the successor of leetcode

2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article focuses on "how to achieve leetcode successors", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "how to achieve leetcode successors"!

Design an algorithm to find out the "next" node of the specified node in the binary search tree (that is, mid-order successor).

Returns null if the specified node does not have a corresponding "next" node.

Example 1:

Input: root = [2, 1, 1, 3], p = 1

two

/\

1 3

Output: 2

Example 2:

Input: root = [5, 3, 7, 6, 6, 2, 4, and 4], p = 6.

five

/\

3 6

/\

2 4

/

one

Output: null

Ideas for solving the problem:

1, similar to the search of a binary search tree, the difference is to find the next node of the current value

2. If p.Value > = root.Value, the successor node must be in the right subtree

3, if p.Value0 {root= Q [l-1] QQ [: l-1:l-1] / / fmt.Println (len (Q), root,root==nil) r=append (rmae root) root=root.Right

}} else {r=append (rjinger root) / / fmt.Println (len (Q), root,root.Right) root=root.Right}} for iVOB0BILITY

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