Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to realize the successor of leetcode

Shulou Source: shulou.com Published: 2022-06-01 13:08:51 09月12日 Update

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

Tags: Nodes successors subtrees successors content examples or recursion learning search input output practical deeper middle finger interest practicality practical ideas simple operation Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info Apple OPPO Reno Linux Shulou Technology