Given a Binary Search Tree that contains positive integer values greater than 0. The task is to check whether the BST contains a dead end or not. Here Dead End means, we are not able to insert any integer element after that node.
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.
We have discussed a solution in below post.
The idea in this post is based on method 3 of Check if a binary tree is BST or not.
First of all, it is given that it is a BST and nodes are greater than zero, root node can be in the range [1, ∞] and if root val is say, val, then left sub-tree can have the value in the range [1, val-1] and right sub-tree the value in range [val+1, ∞].
we need to traverse recursively and when the the min and max value of range coincided it means that we cannot add any node further in the tree.
Hence we encounter a dead end.
Following is the simple recursive solution to the problem.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- A program to check if a binary tree is BST or not
- Check if each internal node of a BST has exactly one child
- Check for Identical BSTs without building the trees
- Check if a given Binary Tree is height balanced like a Red-Black Tree
- How to check if a given array represents a Binary Heap?
- Check if a given Binary Tree is Heap
- Check if a given array can represent Preorder Traversal of Binary Search Tree
- Check if given sorted sub-sequence exists in binary search tree
- Check whether BST contains Dead End or not
- Check if an array represents Inorder of Binary Search tree or not
- Check if two BSTs contain same set of elements
- Check given array of size n can represent BST of n levels or not
- Check if two given key sequences construct same BSTs
- Check whether the two Binary Search Trees are Identical or Not
- Check if a triplet with given sum exists in BST
- Check if a Binary Tree is BST : Simple and Efficient Approach
- Check if the Binary Tree contains a balanced BST of size K
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.