给定二进制搜索树,任务是找到具有最大值的节点。
例子:
Input:
Output: 22
方法:只需递归地从根到节点遍历该节点,直到right为NULL。右侧为NULL的节点是具有最大值的节点。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
/* A binary tree node has data, pointer to left child
and a pointer to right child */
struct node {
int data;
struct node* left;
struct node* right;
};
/* Helper function that allocates a new node
with the given data and NULL left and right
pointers. */
struct node* newNode(int data)
{
struct node* node = (struct node*)
malloc(sizeof(struct node));
node->data = data;
node->left = NULL;
node->right = NULL;
return (node);
}
/* Give a binary search tree and a number,
inserts a new node with the given number in
the correct place in the tree. Returns the new
root pointer which the caller should then use
(the standard trick to avoid using reference
parameters). */
struct node* insert(struct node* node, int data)
{
/* 1. If the tree is empty, return a new,
single node */
if (node == NULL)
return (newNode(data));
else {
/* 2. Otherwise, recur down the tree */
if (data <= node->data)
node->left = insert(node->left, data);
else
node->right = insert(node->right, data);
/* return the (unchanged) node pointer */
return node;
}
}
// Function to return the minimum node
// in the given binary search tree
int maxValue(struct node* node)
{
if (node->right == NULL)
return node->data;
return maxValue(node->right);
}
// Driver code
int main()
{
// Create the BST
struct node* root = NULL;
root = insert(root, 4);
insert(root, 2);
insert(root, 1);
insert(root, 3);
insert(root, 6);
insert(root, 5);
cout << maxValue(root);
return 0;
}
Java
// Java implementation of the approach
class GFG
{
/* A binary tree node has data,
pointer to left child and a
pointer to right child */
static class node
{
int data;
node left;
node right;
};
/* Helper function that allocates a new node
with the given data and null left and right
pointers. */
static node newNode(int data)
{
node node = new node();
node.data = data;
node.left = null;
node.right = null;
return (node);
}
/* Give a binary search tree and a number,
inserts a new node with the given number in
the correct place in the tree. Returns the new
root pointer which the caller should then use
(the standard trick to avoid using reference
parameters). */
static node insert(node node, int data)
{
/* 1. If the tree is empty, return a new,
single node */
if (node == null)
return (newNode(data));
else
{
/* 2. Otherwise, recur down the tree */
if (data <= node.data)
node.left = insert(node.left, data);
else
node.right = insert(node.right, data);
/* return the (unchanged) node pointer */
return node;
}
}
// Function to return the minimum node
// in the given binary search tree
static int maxValue(node node)
{
if (node.right == null)
return node.data;
return maxValue(node.right);
}
// Driver code
public static void main(String args[])
{
// Create the BST
node root = null;
root = insert(root, 4);
root = insert(root, 2);
root = insert(root, 1);
root = insert(root, 3);
root = insert(root, 6);
root = insert(root, 5);
System.out.println(maxValue(root));
}
}
// This code is contributed by Arnab Kundu
Python3
# Python3 implementation of the approach
# A binary tree node has data,
# pointer to left child and
# a pointer to right child
# Linked list node
class Node:
def __init__(self, data):
self.data = data
self.left = None
self.right = None
# Helper function that allocates
# a new node with the given data
# and None left and right pointers.
def newNode(data):
node = Node(0)
node.data = data
node.left = None
node.right = None
return (node)
# Give a binary search tree and a number,
# inserts a new node with the given number in
# the correct place in the tree. Returns the new
# root pointer which the caller should then use
# (the standard trick to avoid using reference
# parameters).
def insert(node,data):
# 1. If the tree is empty,
# return a new, single node
if (node == None):
return (newNode(data))
else :
# 2. Otherwise, recur down the tree
if (data <= node.data):
node.left = insert(node.left, data)
else:
node.right = insert(node.right, data)
# return the (unchanged) node pointer */
return node
# Function to return the minimum node
# in the given binary search tree
def maxValue(node):
if (node.right == None):
return node.data
return maxValue(node.right)
# Driver Code
if __name__=='__main__':
# Create the BST
root = None
root = insert(root, 4)
root = insert(root, 2)
root = insert(root, 1)
root = insert(root, 3)
root = insert(root, 6)
root = insert(root, 5)
print (maxValue(root))
# This code is contributed by Arnab Kundu
C#
// C# implementation of the approach
using System;
class GFG
{
/* A binary tree node has data,
pointer to left child and a
pointer to right child */
public class node
{
public int data;
public node left;
public node right;
};
/* Helper function that allocates a new node
with the given data and null left and right
pointers. */
static node newNode(int data)
{
node node = new node();
node.data = data;
node.left = null;
node.right = null;
return (node);
}
/* Give a binary search tree and a number,
inserts a new node with the given number in
the correct place in the tree. Returns the new
root pointer which the caller should then use
(the standard trick to avoid using reference
parameters). */
static node insert(node node, int data)
{
/* 1. If the tree is empty, return a new,
single node */
if (node == null)
return (newNode(data));
else
{
/* 2. Otherwise, recur down the tree */
if (data <= node.data)
node.left = insert(node.left, data);
else
node.right = insert(node.right, data);
/* return the (unchanged) node pointer */
return node;
}
}
// Function to return the minimum node
// in the given binary search tree
static int maxValue(node node)
{
if (node.right == null)
return node.data;
return maxValue(node.right);
}
// Driver code
public static void Main(String []args)
{
// Create the BST
node root = null;
root = insert(root, 4);
root = insert(root, 2);
root = insert(root, 1);
root = insert(root, 3);
root = insert(root, 6);
root = insert(root, 5);
Console.WriteLine(maxValue(root));
}
}
/* This code contributed by PrinciRaj1992 */
输出:
6
时间复杂度: O(n),最坏的情况发生在右倾斜的树上。