📜  根据给定的遍历遍历查找BST的最左和最右节点

📅  最后修改于: 2021-04-24 05:00:08             🧑  作者: Mango

给定N个节点的二分搜索树的前序序列。任务是找到其最左边和最右边的节点。

例子:

Input : N = 5, preorder[]={ 3, 2, 1, 5, 4 }
Output : Leftmost = 1, Rightmost = 5
The BST constructed from this 
preorder sequence would be:
         3
       /   \
      2     5
     /     /
    1     4
Leftmost Node of this tree is equal to 1
Rightmost Node of this tree is equal to 5

Input : N = 3 preorder[]={ 2, 1, 3}
Output : Leftmost = 1, Rightmost = 3

天真的方法:
从给定的预购序列构造BST请参阅这篇文章,以了解从给定的预购序列构造bst的代码。构造完BST之后,通过从根到最左节点遍历并从根到最右节点遍历找到最左边和最右边的节点。

时间复杂度: O(N)
空间复杂度: O(N)

高效方法:
而不是构造树,而是使用BST的属性。 BST中最左侧的节点始终具有最小值,BST中最右侧的节点始终具有最大值。
因此,从给定的数组中,我们只需要找到最左边的节点的最小值和最右边的节点的最大值。

下面是上述方法的实现

C++
// C++ program to find leftmost and
// rightmost node from given preorder sequence
#include 
using namespace std;
  
// Function to return the leftmost and 
// rightmost nodes of the BST whose 
// preorder traversal is given
void LeftRightNode(int preorder[], int n)
{
    // Variables for finding minimum 
    // and maximum values of the array
    int min = INT_MAX, max = INT_MIN;
  
    for (int i = 0; i < n; i++) {
          
        // Update the minimum
        if (min > preorder[i])
            min = preorder[i];
     
        // Update the maximum
        if (max < preorder[i])
            max = preorder[i];
    }
  
    // Print the values
    cout << "Leftmost node is " << min << "\n";
    cout << "Rightmost node is " << max;
}
  
// Driver Code
int main()
{
    int preorder[] = { 3, 2, 1, 5, 4 };
    int n = 5;
  
    LeftRightNode(preorder, n);
    return 0;
}


Java
// Java program to find leftmost and
// rightmost node from given preorder sequence
class GFG
{
  
// Function to return the leftmost and 
// rightmost nodes of the BST whose 
// preorder traversal is given
static void LeftRightNode(int preorder[], int n)
{
    // Variables for finding minimum 
    // and maximum values of the array
    int min = Integer.MAX_VALUE, max = Integer.MIN_VALUE;
  
    for (int i = 0; i < n; i++)
    {
          
        // Update the minimum
        if (min > preorder[i])
            min = preorder[i];
      
        // Update the maximum
        if (max < preorder[i])
            max = preorder[i];
    }
    // Print the values
    System.out.println("Leftmost node is " + min);
    System.out.println("Rightmost node is " + max);
}
  
// Driver Code
public static void main(String[] args) 
{
        int preorder[] = { 3, 2, 1, 5, 4 };
        int n = 5;
        LeftRightNode(preorder, n);
  
}
}
  
// This code is contributed by 29AjayKumar


Python3
# Python3 program to find leftmost and
# rightmost node from given preorder sequence
  
# Function to return the leftmost and 
# rightmost nodes of the BST whose 
# preorder traversal is given
def LeftRightNode(preorder, n):
    # Variables for finding minimum 
    # and maximum values of the array
    min = 10**9
    max = -10**9
  
    for i in range(n):
          
        # Update the minimum
        if (min > preorder[i]):
            min = preorder[i]
      
        # Update the maximum
        if (max < preorder[i]):
            max = preorder[i]
  
    # Print the values
    print("Leftmost node is ",min)
    print("Rightmost node is ",max)
  
# Driver Code
  
preorder = [3, 2, 1, 5, 4]
n =len(preorder)
  
LeftRightNode(preorder, n)
  
# This code is contributed by mohit kumar 29


C#
// C# program to find leftmost and
// rightmost node from given preorder sequence
using System;
      
class GFG
{
  
// Function to return the leftmost and 
// rightmost nodes of the BST whose 
// preorder traversal is given
static void LeftRightNode(int []preorder, int n)
{
    // Variables for finding minimum 
    // and maximum values of the array
    int min = int.MaxValue, max = int.MinValue;
  
    for (int i = 0; i < n; i++)
    {
          
        // Update the minimum
        if (min > preorder[i])
            min = preorder[i];
      
        // Update the maximum
        if (max < preorder[i])
            max = preorder[i];
    }
    // Print the values
    Console.WriteLine("Leftmost node is " + min);
    Console.WriteLine("Rightmost node is " + max);
}
  
// Driver Code
public static void Main(String[] args) 
{
        int []preorder = { 3, 2, 1, 5, 4 };
        int n = 5;
        LeftRightNode(preorder, n);
  
}
}
  
// This code is contributed by Rajput-Ji


输出:
Leftmost node is 1
Rightmost node is 5

时间复杂度: O(N)
空间复杂度: O(1)