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 }
  • 相关阅读:
    过用户层HOOK思路
    Linux LVM实践
    matlab演奏卡农 Cripple Pachebel's Canon on Matlab
    rman备份恢复总结
    郁金香VC外挂教程(全) 翻录版 免Key(精品教程)
    C# string 中的 @ 作用处理\等字符
    (抓)2分法通用存储过程分页(top max模式)版本(性能相对之前的not in版本极大提高)
    怎样应用OracleParameter怎样写like查询语句?
    (转)DirectoryEntry的使用
    解决模式对话框和window.open打开新页面Session会丢失问题
  • 原文地址:https://www.cnblogs.com/lz87/p/7292392.html
Copyright © 2011-2022 走看看