09. BST with Dead End
โ GFG solution to BST with Dead End: detect if a BST has any dead-end node where no more insertions are possible while maintaining BST properties. ๐
The problem can be found at the following link: ๐ Question Link
๐งฉ Problem Description
You are given a Binary Search Tree (BST) that contains only unique positive integers greater than 0. A dead end is a leaf node such that no further node can be inserted under it while still maintaining the BST property and constraint that node values must be > 0.
Your task is to determine whether the BST contains any such dead end.
๐ Examples
Example 1
Input: root[] = [8, 5, 9, 2, 7, N, N, 1]
Output: true
Explanation: Node 1 is a dead end since 0 is not allowed and 2 already exists.
Example 2
Input: root[] = [8, 7, 10, 2, N, 9, 13]
Output: true
Explanation: Node 9 is a dead end as no new node can be inserted below it.
๐ Constraints
$1 \leq \text{Number of nodes} \leq 3000$
$1 \leq \text{Node->data} \leq 10^5$
โ
My Approach
DFS with Dynamic Range [Low, High]
We recursively traverse the tree, carrying two bounds:
l: the lowest possible value a child node can taker: the highest possible value a child node can take
If we encounter a leaf node where l == r, it means:
There is no space left to insert any node under this leaf while obeying BST rules and positivity constraint.
๐ก Logic:
Initially call:
dfs(root, 1, INT_MAX)For each node:
Check left:
dfs(root->left, l, root->data - 1)Check right:
dfs(root->right, root->data + 1, r)
If
l == rat a leaf โ Dead End found.
๐ Time and Auxiliary Space Complexity
Expected Time Complexity: O(n), as we visit each node exactly once.
Expected Auxiliary Space Complexity: O(h), where
his the height of the BST due to recursion stack.
๐งโ๐ป Code (C++)
โก View Alternative Approaches with Code and Analysis
๐ 2๏ธโฃ HashSet + Inorder Traversal
๐ก Algorithm Steps:
Traverse the BST and store all nodes and leaf nodes in
unordered_set<int>:allfor every nodeleavesfor leaf nodes
After traversal, check for each leaf if
leaf+1andleaf-1both exist inallset โ this indicates a dead end.
๐ Complexity Analysis:
Time: โฑ๏ธ O(n)
Space: ๐พ O(n) (due to sets)
โ
Why This Approach?
Tracks all nodes and leaves to verify dead ends directly.
Effective when recursive range-check logic is hard to trace.
๐ 3๏ธโฃ Bottom-Up BST Recursion with Early Return
๐ก Algorithm Steps:
Use recursion to return
trueif dead end exists in either left or right subtree.At each node, check if
min == max(boundary restriction met).
๐ Complexity Analysis:
Time: โฑ๏ธ O(n)
Space: ๐พ O(h) (recursive stack)
โ
Why This Approach?
Clean, concise, and leverages BST property via boundary propagation.
Matches the original reference approach.
๐ ๐ Comparison of Approaches
๐ Approach
โฑ๏ธ Time
๐พ Space
โ Pros
โ ๏ธ Cons
๐ DFS with Boundaries
๐ข O(n)
๐ข O(h)
Efficient, no extra memory
Requires correct range management
๐ฆ HashSet + Traverse
๐ข O(n)
๐ข O(n)
Simple logic with pre-checking
Uses more memory
๐งฎ Bottom-Up Recursion
๐ข O(n)
๐ก O(h)
Clear condition checking
Conceptually similar to 1st but less direct
๐ Best Choice Recommendation
๐ฏ Scenario
๐๏ธ Recommended Approach
๐ For concise & optimal solution
๐ฅ DFS with boundaries
๐งช For readability and debugging ease
๐ฅ HashSet leaf checking
๐ When working with deep tree logic
๐ฅ Bottom-Up recursion
๐งโ๐ป Code (Java)
๐ Code (Python)
๐ง Contribution and Support
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: ๐ฌ Any Questions?. Letโs make this learning journey more collaborative!
โญ If you find this helpful, please give this repository a star! โญ
๐Visitor Count
Last updated