给定一个二进制搜索树,其中包含大于0的正整数值。任务是检查BST是否包含死角。此处的“死角”意味着,我们无法在该节点之后插入任何整数元素。
例子:
Input : 8
/ \
5 9
/ \
2 7
/
1
Output : Yes
Explanation : Node "1" is the dead End because
after that we cant insert any element.
Input : 8
/ \
7 10
/ / \
2 9 13
Output :Yes
Explanation : We can't insert any element at
node 9.
我们已经在下面的文章中讨论了一个解决方案。
检查BST是否包含死角
这篇文章中的想法基于检查二进制树是否为BST的方法3。
首先,假设它是一个BST,并且节点大于零,根节点的范围可以为[1,∞],如果说根val为val,则左子树的值可以为范围[1,val-1]和右子树的值在范围[val + 1,∞]中。
我们需要递归遍历,并且当范围的最小值和最大值重合时,这意味着我们无法在树中进一步添加任何节点。
因此,我们遇到了死胡同。
以下是该问题的简单递归解决方案。
C++
// CPP Program to check if there is a dead end
// in BST or not.
#include
using namespace std;
// A BST node
struct Node {
int data;
struct Node *left, *right;
};
// A utility function to create a new node
Node* newNode(int data)
{
Node* temp = new Node;
temp->data = data;
temp->left = temp->right = NULL;
return temp;
}
/* A utility function to insert a new Node
with given key in BST */
struct Node* insert(struct Node* node, int key)
{
/* If the tree is empty, return a new Node */
if (node == NULL)
return newNode(key);
/* Otherwise, recur down the tree */
if (key < node->data)
node->left = insert(node->left, key);
else if (key > node->data)
node->right = insert(node->right, key);
/* return the (unchanged) Node pointer */
return node;
}
// Returns true if tree with given root contains
// dead end or not. min and max indicate range
// of allowed values for current node. Initially
// these values are full range.
bool deadEnd(Node* root, int min=1, int max=INT_MAX)
{
// if the root is null or the recursion moves
// after leaf node it will return false
// i.e no dead end.
if (!root)
return false;
// if this occurs means dead end is present.
if (min == max)
return true;
// heart of the recursion lies here.
return deadEnd(root->left, min, root->data - 1) ||
deadEnd(root->right, root->data + 1, max);
}
// Driver program
int main()
{
/* 8
/ \
5 11
/ \
2 7
\
3
\
4 */
Node* root = NULL;
root = insert(root, 8);
root = insert(root, 5);
root = insert(root, 2);
root = insert(root, 3);
root = insert(root, 7);
root = insert(root, 11);
root = insert(root, 4);
if (deadEnd(root) == true)
cout << "Yes " << endl;
else
cout << "No " << endl;
return 0;
}
Java
// Java Program to check if there
// is a dead end in BST or not.
class BinarySearchTree {
// Class containing left and right
// child of current node and key value
class Node {
int data;
Node left, right;
public Node(int item) {
data = item;
left = right = null;
}
}
// Root of BST
Node root;
// Constructor
BinarySearchTree() {
root = null;
}
// This method mainly calls insertRec()
void insert(int data) {
root = insertRec(root, data);
}
// A recursive function
// to insert a new key in BST
Node insertRec(Node root, int data) {
// If the tree is empty,
// return a new node
if (root == null) {
root = new Node(data);
return root;
}
/* Otherwise, recur down the tree */
if (data < root.data)
root.left = insertRec(root.left, data);
else if (data > root.data)
root.right = insertRec(root.right, data);
/* return the (unchanged) node pointer */
return root;
}
// Returns true if tree with given root contains
// dead end or not. min and max indicate range
// of allowed values for current node. Initially
// these values are full range.
boolean deadEnd(Node root, int min, int max)
{
// if the root is null or the recursion moves
// after leaf node it will return false
// i.e no dead end.
if (root==null)
return false;
// if this occurs means dead end is present.
if (min == max)
return true;
// heart of the recursion lies here.
return deadEnd(root.left, min, root.data - 1)||
deadEnd(root.right, root.data + 1, max);
}
// Driver Program
public static void main(String[] args) {
BinarySearchTree tree = new BinarySearchTree();
/* 8
/ \
5 11
/ \
2 7
\
3
\
4 */
tree.insert(8);
tree.insert(5);
tree.insert(2);
tree.insert(3);
tree.insert(7);
tree.insert(11);
tree.insert(4);
if (tree.deadEnd(tree.root ,1 ,
Integer.MAX_VALUE) == true)
System.out.println("Yes ");
else
System.out.println("No " );
}
}
// This code is contributed by Gitanjali.
Python3
# Python 3 Program to check if there
# is a dead end in BST or not.
class Node:
# Constructor to create a new node
def __init__(self, data):
self.data = data
self.left = None
self.right = None
# A utility function to insert a
# new Node with given key in BST
def insert(node, key):
# If the tree is empty,
# return a new Node
if node == None:
return Node(key)
# Otherwise, recur down the tree
if key < node.data:
node.left = insert(node.left, key)
elif key > node.data:
node.right = insert(node.right, key)
# return the (unchanged) Node pointer
return node
# Returns true if tree with given
# root contains dead end or not.
# min and max indicate range
# of allowed values for current node.
# Initially these values are full range.
def deadEnd(root, Min, Max):
# if the root is null or the recursion
# moves after leaf node it will return
# false i.e no dead end.
if root == None:
return False
# if this occurs means dead
# end is present.
if Min == Max:
return True
# heart of the recursion lies here.
return (deadEnd(root.left, Min, root.data - 1) or
deadEnd(root.right, root.data + 1, Max))
# Driver Code
if __name__ == '__main__':
# 8
# / \
# 5 11
# / \
# 2 7
# \
# 3
# \
# 4
root = None
root = insert(root, 8)
root = insert(root, 5)
root = insert(root, 2)
root = insert(root, 3)
root = insert(root, 7)
root = insert(root, 11)
root = insert(root, 4)
if deadEnd(root, 1, 9999999999) == True:
print("Yes")
else:
print("No")
# This code is contributed by PranchalK
C#
using System;
// C# Program to check if there
// is a dead end in BST or not.
public class BinarySearchTree
{
// Class containing left and right
// child of current node and key value
public class Node
{
private readonly BinarySearchTree outerInstance;
public int data;
public Node left, right;
public Node(BinarySearchTree outerInstance, int item)
{
this.outerInstance = outerInstance;
data = item;
left = right = null;
}
}
// Root of BST
public Node root;
// Constructor
public BinarySearchTree()
{
root = null;
}
// This method mainly calls insertRec()
public virtual void insert(int data)
{
root = insertRec(root, data);
}
// A recursive function
// to insert a new key in BST
public virtual Node insertRec(Node root, int data)
{
// If the tree is empty,
// return a new node
if (root == null)
{
root = new Node(this, data);
return root;
}
/* Otherwise, recur down the tree */
if (data < root.data)
{
root.left = insertRec(root.left, data);
}
else if (data > root.data)
{
root.right = insertRec(root.right, data);
}
/* return the (unchanged) node pointer */
return root;
}
// Returns true if tree with given root contains
// dead end or not. min and max indicate range
// of allowed values for current node. Initially
// these values are full range.
public virtual bool deadEnd(Node root, int min, int max)
{
// if the root is null or the recursion moves
// after leaf node it will return false
// i.e no dead end.
if (root == null)
{
return false;
}
// if this occurs means dead end is present.
if (min == max)
{
return true;
}
// heart of the recursion lies here.
return deadEnd(root.left, min, root.data - 1) || deadEnd(root.right, root.data + 1, max);
}
// Driver Program
public static void Main(string[] args)
{
BinarySearchTree tree = new BinarySearchTree();
/* 8
/ \
5 11
/ \
2 7
\
3
\
4 */
tree.insert(8);
tree.insert(5);
tree.insert(2);
tree.insert(3);
tree.insert(7);
tree.insert(11);
tree.insert(4);
if (tree.deadEnd(tree.root,1, int.MaxValue) == true)
{
Console.WriteLine("Yes ");
}
else
{
Console.WriteLine("No ");
}
}
}
// This code is contributed by Shrikant13
输出:
Yes