给定二进制搜索树和数字N,任务是在二进制搜索树中找到大于或等于N的最小数字。如果元素存在,则打印该元素的值,否则打印-1。
例子:
Input: N = 20
Output: 21
Explanation: 21 is the smallest element greater than 20.
Input: N = 18
Output: 19
Explanation: 19 is the smallest element greater than 18.
方法:
想法是遵循递归方法来解决问题,即从根开始搜索元素。
- 如果有一个叶子节点的值小于N,则元素不存在并返回-1。
- 否则,如果节点的值大于或等于N,并且左子节点为NULL或小于N,则返回节点值。
- 否则,如果节点的值小于N,则在右侧子树中搜索该元素。
- 否则,通过根据左值或右值递归调用函数,在左子树中搜索元素。
C++
// C++ program to find the smallest value
// greater than or equal to N
#include
using namespace std;
struct Node {
int data;
Node *left, *right;
};
// To create new BST Node
Node* createNode(int item)
{
Node* temp = new Node;
temp->data = item;
temp->left = temp->right = NULL;
return temp;
}
// To add a new node in BST
Node* add(Node* node, int key)
{
// if tree is empty return new node
if (node == NULL)
return createNode(key);
// if key is less then or grater then
// node value then recur down the tree
if (key < node->data)
node->left = add(node->left, key);
else if (key > node->data)
node->right = add(node->right, key);
// return the (unchanged) node pointer
return node;
}
// function to find min value less then N
int findMinforN(Node* root, int N)
{
// If leaf node reached and is smaller than N
if (root->left == NULL && root->right == NULL
&& root->data < N)
return -1;
// If node's value is greater than N and left value
// is NULL or smaller then return the node value
if ((root->data >= N && root->left == NULL)
|| (root->data >= N && root->left->data < N))
return root->data;
// if node value is smaller than N search in the
// right subtree
if (root->data <= N)
return findMinforN(root->right, N);
// if node value is greater than N search in the
// left subtree
else
return findMinforN(root->left, N);
}
// Drivers code
int main()
{
/* 19
/ \
7 21
/ \
3 11
/ \
9 14
*/
Node* root = NULL;
root = add(root, 19);
root = add(root, 7);
root = add(root, 3);
root = add(root, 11);
root = add(root, 9);
root = add(root, 13);
root = add(root, 21);
int N = 18;
cout << findMinforN(root, N) << endl;
return 0;
}
Java
// Java program to find the smallest value
// greater than or equal to N
import java.util.*;
class GFG
{
static class Node
{
int data;
Node left, right;
};
// To create new BST Node
static Node createNode(int item)
{
Node temp = new Node();
temp.data = item;
temp.left = temp.right = null;
return temp;
}
// To add a new node in BST
static Node add(Node node, int key)
{
// if tree is empty return new node
if (node == null)
return createNode(key);
// if key is less then or grater then
// node value then recur down the tree
if (key < node.data)
node.left = add(node.left, key);
else if (key > node.data)
node.right = add(node.right, key);
// return the (unchanged) node pointer
return node;
}
// function to find min value less then N
static int findMinforN(Node root, int N)
{
// If leaf node reached and is smaller than N
if (root.left == null &&
root.right == null && root.data < N)
return -1;
// If node's value is greater than N and left value
// is null or smaller then return the node value
if ((root.data >= N && root.left == null) ||
(root.data >= N && root.left.data < N))
return root.data;
// if node value is smaller than N search in the
// right subtree
if (root.data <= N)
return findMinforN(root.right, N);
// if node value is greater than N search in the
// left subtree
else
return findMinforN(root.left, N);
}
// Driver Code
public static void main(String[] args)
{
/* 19
/ \
7 21
/ \
3 11
/ \
9 14
*/
Node root = null;
root = add(root, 19);
root = add(root, 7);
root = add(root, 3);
root = add(root, 11);
root = add(root, 9);
root = add(root, 13);
root = add(root, 21);
int N = 18;
System.out.println(findMinforN(root, N));
}
}
// This code is contributed by Rajput-Ji
Python3
# Python program to find the smallest value
# greater than or equal to N
class Node:
def __init__(self):
self.data = 0
self.left = None
self.right = None
# To create new BST Node
def createNode(item: int):
temp = Node()
temp.data = item
temp.left = temp.right = None
return temp
# To add a new node in BST
def add(node: Node, key: int):
# if tree is empty return new node
if node is None:
return createNode(key)
# if key is less then or grater then
# node value then recur down the tree
if key < node.data:
node.left = add(node.left, key)
elif key > node.data:
node.right = add(node.right, key)
# return the (unchanged) node pointer
return node
# function to find min value less then N
def findMinforN(root: Node, N: int):
# If leaf node reached and is smaller than N
if root.left is None and root.right is None and root.data < N:
return -1
# If node's value is greater than N and left value
# is NULL or smaller then return the node value
if root.data >= N and root.left is None or root.data >= N and root.left.data < N:
return root.data
# if node value is smaller than N search in the
# right subtree
if root.data <= N:
return findMinforN(root.right, N)
# if node value is greater than N search in the
# left subtree
else:
return findMinforN(root.left, N)
# Driver Code
if __name__ == "__main__":
'''
19
/ \
7 21
/ \
3 11
/ \
9 14
'''
root = Node()
root = add(root, 19)
root = add(root, 7)
root = add(root, 3)
root = add(root, 11)
root = add(root, 9)
root = add(root, 13)
root = add(root, 21)
N = 18
print(findMinforN(root, N))
# This code is contributed by
# sanjeev2552
C#
// C# program to find the smallest value
// greater than or equal to N
using System;
class GFG
{
class Node
{
public int data;
public Node left, right;
};
// To create new BST Node
static Node createNode(int item)
{
Node temp = new Node();
temp.data = item;
temp.left = temp.right = null;
return temp;
}
// To add a new node in BST
static Node add(Node node, int key)
{
// if tree is empty return new node
if (node == null)
return createNode(key);
// if key is less then or grater then
// node value then recur down the tree
if (key < node.data)
node.left = add(node.left, key);
else if (key > node.data)
node.right = add(node.right, key);
// return the (unchanged) node pointer
return node;
}
// function to find min value less then N
static int findMinforN(Node root, int N)
{
// If leaf node reached and is smaller than N
if (root.left == null &&
root.right == null && root.data < N)
return -1;
// If node's value is greater than N and left value
// is null or smaller then return the node value
if ((root.data >= N && root.left == null) ||
(root.data >= N && root.left.data < N))
return root.data;
// if node value is smaller than N search in the
// right subtree
if (root.data <= N)
return findMinforN(root.right, N);
// if node value is greater than N search in the
// left subtree
else
return findMinforN(root.left, N);
}
// Driver Code
public static void Main(String[] args)
{
/* 19
/ \
7 21
/ \
3 11
/ \
9 14
*/
Node root = null;
root = add(root, 19);
root = add(root, 7);
root = add(root, 3);
root = add(root, 11);
root = add(root, 9);
root = add(root, 13);
root = add(root, 21);
int N = 18;
Console.WriteLine(findMinforN(root, N));
}
}
// This code is contributed by Princi Singh
输出:
19