[Swift]LeetCode285. 二叉搜索树中的中序后继节点 $ Inorder Successor in BST

Given a binary search tree and a node in it, find the in-order successor of that node in the BST.

The successor of a node p is the node with the smallest key greater than p.val.

Example 1:

Input: root = [2,1,3], p = 1
Output: 2
Explanation: 1‘s in-order successor node is 2. Note that both p and the return value is of TreeNode type.

Example 2:

Input: root = [5,3,6,2,4,null,null,1], p = 6
Output: null
Explanation: There is no in-order successor of the current node, so the answer is null.

Note:

  1. If the given node has no in-order successor in the tree, return null.
  2. It‘s guaranteed that the values of the tree are unique.


给定一个二进制搜索树及其节点,在BST中查找该节点的顺序继承者。

节点p的后续节点是键最小大于p.val的节点。

例 1:

输入: root = [2,1,3], p = 1
输出: 2
说明:1的顺序继承节点为2。请注意,p和返回值都是treenode类型。

例  2:

输入: root = [5,3,6,2,4,null,null,1], p = 6
输出: null说明:当前节点没有按顺序的后续节点,因此答案为空。

注:

  1. 如果给定的节点在树中没有顺序继承者,则返回空。
  2. 它保证了树的值是唯一的。


Solution:

 1 public class TreeNode {
 2     public var val: Int
 3     public var left: TreeNode?
 4     public var right: TreeNode?
 5     public init(_ val: Int) {
 6         self.val = val
 7         self.left = nil
 8         self.right = nil
 9     }
10 }
11
12 class Solution {
13     func inorderSuccessor(_ root: TreeNode?,_ p: TreeNode?) -> TreeNode? {
14         var root = root
15         var res:TreeNode? = nil
16         while(root != nil)
17         {
18             if root!.val > p!.val
19             {
20                 res = root
21                 root = root?.left
22             }
23             else
24             {
25                 root = root?.right
26             }
27         }
28         return res
29     }
30 }

原文地址:https://www.cnblogs.com/strengthen/p/10686067.html

时间: 2024-08-01 06:43:48

[Swift]LeetCode285. 二叉搜索树中的中序后继节点 $ Inorder Successor in BST的相关文章

[LeetCode] 285. Inorder Successor in BST 二叉搜索树中的中序后继节点

Given a binary search tree and a node in it, find the in-order successor of that node in the BST. Note: If the given node has no in-order successor in the tree, return null. 给一个二叉搜索树和它的一个节点,找出它的中序后继节点,如果没有返回null. 解法1: 用中序遍历二叉搜索树,当找到root.val = p.val的时

[LeetCode] Inorder Successor in BST II 二叉搜索树中的中序后继节点之二

Given a binary search tree and a node in it, find the in-order successor of that node in the BST. The successor of a node p is the node with the smallest key greater than p.val. You will have direct access to the node but not to the root of the tree.

[Swift]LeetCode501. 二叉搜索树中的众数 | Find Mode in Binary Search Tree

Given a binary search tree (BST) with duplicates, find all the mode(s) (the most frequently occurred element) in the given BST. Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than or equal to the nod

[Swift]LeetCode450. 删除二叉搜索树中的节点 | Delete Node in a BST

Given a root node reference of a BST and a key, delete the node with the given key in the BST. Return the root node reference (possibly updated) of the BST. Basically, the deletion can be divided into two stages: Search for a node to remove. If the n

二叉搜索树中的常用方法

1 package Tree; 2 3 import org.junit.Test; 4 5 class TreeNode { 6 7 int val = 0; 8 TreeNode left = null; 9 TreeNode right = null; 10 11 public TreeNode(int val) { 12 this.val = val; 13 14 } 15 16 } 17 18 public class BinarySearchTree { 19 20 /** 21 *

230. 二叉搜索树中第K小的元素

230. 二叉搜索树中第K小的元素 题意 给定一个二叉搜索树,编写一个函数 kthSmallest 来查找其中第 k 个最小的元素. 你可以假设 k 总是有效的,1 ≤ k ≤ 二叉搜索树元素个数. 解题思路 中序遍历,利用Python3中提供的生成器方法: 中序遍历,判断存储结点值的数组是否到到k,则表明访问的一个结点就是第k个最小的元素: 先获取跟结点处于的位置(第几个最小的元素),如果它比k小,则从右子结点中找,如果它比k大,则从左子节点中找: 实现 class Solution:    

算法dfs——二叉搜索树中最接近的值 II

901. 二叉搜索树中最接近的值 II 中文 English 给定一棵非空二叉搜索树以及一个target值,找到 BST 中最接近给定值的 k 个数. 样例 样例 1: 输入: {1} 0.000000 1 输出: [1] 解释: 二叉树 {1},表示如下的树结构: 1 样例 2: 输入: {3,1,4,#,2} 0.275000 2 输出: [1,2] 解释: 二叉树 {3,1,4,#,2},表示如下的树结构: 3 / 1 4 2 挑战 假设是一棵平衡二叉搜索树,你可以用时间复杂度低于O(n)

leetcode 二叉搜索树中第K小的元素 python

二叉搜索树中第K小的元素 给定一个二叉搜索树,编写一个函数 kthSmallest 来查找其中第 k 个最小的元素. 说明:你可以假设 k 总是有效的,1 ≤ k ≤ 二叉搜索树元素个数. 示例 1: 输入: root = [3,1,4,null,2], k = 1 3 / 1 4   2 输出: 1 示例 2: 输入: root = [5,3,6,2,4,null,null,1], k = 3 5 / 3 6 / 2 4 / 1 输出: 3 进阶:如果二叉搜索树经常被修改(插入/删除操作)并且

Leetcode 701. 二叉搜索树中的插入操作

题目链接 https://leetcode.com/problems/insert-into-a-binary-search-tree/description/ 题目描述 给定二叉搜索树(BST)的根节点和要插入树中的值,将值插入二叉搜索树. 返回插入后二叉搜索树的根节点. 保证原始二叉搜索树中不存在新值. 注意,可能存在多种有效的插入方式,只要树在插入后仍保持为二叉搜索树即可. 你可以返回任意有效的结果. 例如, 给定二叉搜索树: 4 / 2 7 / 1 3 和 插入的值: 5 你可以返回这个