![]() |
VOOZH | about |
Given a Binary Tree and a positive integer K. The task is to check whether the Balanced BST of size K exists in a given Binary Tree or not. If it exists then print βYesβ else print βNoβ.
Examples:
Input: K = 4, Below is the given Tree: 15 / \ 10 26 / \ / \ 5 12 25 40 / / \ 20 35 50 \ 60 Output: Yes Explanation: Subtree of the given tree with size k is given below: 40 / \ 35 50 \ 60 Input: K = 4, Below is the given Tree: 18 / 9 / \ 7 10 Output: No Explanation: There is no subtree of size K which forms a balanced BT.
Approach: The idea is to use the Post Order Traversal. The following are the steps for solving the problem:
Below is the implementation of the above approach:
Yes
Time Complexity: O(N)
Auxiliary Space: O(1)