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 }
  • 相关阅读:
    [R] read.table的check.names参数防止读入数据时列名前自动加上"X."
    【宏基因组】MEGAN4,MEGAN5和MEGAN6的Linux安装和使用
    洛谷—— P1077 摆花
    洛谷—— P2733 家的范围 Home on the Range
    BZOJ——T 1801: [Ahoi2009]chess 中国象棋
    洛谷—— P1379 八数码难题
    BZOJ——T 1800: [Ahoi2009]fly 飞行棋
    几种outofmemory
    几种常见web攻击手段及其防御方式
    JVM参数
  • 原文地址:https://www.cnblogs.com/lz87/p/7292392.html
Copyright © 2011-2022 走看看