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 }
  • 相关阅读:
    html实现 省——市——区三级联动
    test
    JAVA课程设计——坦克大战
    Java MOOC-互评作业-流与文件
    DS博客作业06--图
    DS博客作业08--课程总结
    DS博客作业07--查找
    DS博客作业06--图
    DS博客作业05-树
    DS博客作业01--日期抽象数据类型设计与实现
  • 原文地址:https://www.cnblogs.com/lz87/p/7292392.html
Copyright © 2011-2022 走看看