zoukankan      html  css  js  c++  java
  • [GeeksForGeeks] Inorder Predecessor and Successor for a given key in BST

    Given a BST whose keys are integers.  Find the inorder successor and predecessor of a given key. In case the given key 

    is not found in the BST, return the two values within which this key will reside.

    Solution. 

    If either the inorder successor or the predecessor is not found,  null is returned to indicate this. 

    For example,

    1. no nodes in bst, [null, null] is returned;

    2. only 1 node in bst, if root.val == target, [null, null] is returned;

     if root.val < target, [root, null] is returned;

     if root.val > target, [null, root] is returned. 

    Recursion algorithm:

    1. base case, if root == null, return;

    2. if root.val == target, set predecessor to the biggest node of root's left subtree, if root has no left subtree, predecessor is set to null;

     then set successor to the smallest node of root's right subtree, if root has no right subtree, successor is set to null;

    3. if root.val < target,  set predecessor to root, then recursively find predecessor and successor on root's right subtree;

    4. if root.val > target, set successor to root, then recursively find predecessor and successor on root's left subtree.

     1 class TreeNode {
     2     TreeNode left;
     3     TreeNode right;
     4     int val;
     5     TreeNode(int val){
     6         this.left = null;
     7         this.right = null;
     8         this.val = val;
     9     }
    10 }
    11 public class Solution {
    12     public TreeNode[] findInorderPreAndSucInBst(TreeNode root, int target) {
    13         if(root == null){
    14             return null;
    15         }
    16         TreeNode[] range = {null, null};
    17         findInorderHelper(root, target, range);
    18         return range;
    19     }
    20     private void findInorderHelper(TreeNode node, int target, TreeNode[] range){
    21         if(node == null){
    22             return;
    23         }
    24         if(node.val == target){
    25             if(node.left != null){
    26                 TreeNode tmp = node.left;
    27                 while(tmp.right != null){
    28                     tmp = tmp.right;
    29                 }
    30                 range[0] = tmp;
    31             }
    32             if(node.right != null){
    33                 TreeNode tmp = node.right;
    34                 while(tmp.left != null){
    35                     tmp = tmp.left;
    36                 }
    37                 range[1] = tmp;
    38             }
    39         }
    40         else if(node.val > target){
    41             range[1] = node;
    42             findInorderHelper(node.left, target, range);
    43         }
    44         else{
    45             range[0] = node;
    46             findInorderHelper(node.right, target, range);            
    47         }
    48     }
    49 }
  • 相关阅读:
    speex编译
    c语言字符函数
    AndroidStudio打包jar
    Android Studio配置及使用OpenCV
    Android Studio CMake依赖第三方库
    Android Studio CMake 生成多个so
    android 闹钟设置问题
    linux进程间的通信之 共享内存
    随机森林为什么要不适用所有特征
    Spark架构与作业执行流程简介
  • 原文地址:https://www.cnblogs.com/lz87/p/7292392.html
Copyright © 2011-2022 走看看