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.
- Check whether BST contains Dead End or not
- Check if a Binary Tree is BST : Simple and Efficient Approach
- Check if the Binary Tree contains a balanced BST of size K
- Two nodes of a BST are swapped, correct the BST | Set-2
- K'th Largest Element in BST when modification to BST is not allowed
- Find k-th smallest element in BST (Order Statistics in BST)
- Convert a normal BST to Balanced BST
- Two nodes of a BST are swapped, correct the BST
- 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 given array of size n can represent BST of n levels or not
- Check if a triplet with given sum exists in BST
- Check whether the two Binary Search Trees are Identical or Not
- Find the largest BST subtree in a given Binary Tree | Set 1
- Sorted order printing of a given array that represents a BST
- Print BST keys in the given range
- Sorted Linked List to Balanced BST
- Sorted Array to Balanced BST
- K'th smallest element in BST using O(1) Extra Space
- Construct BST from given preorder traversal | Set 2
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.