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 Post Order Traversal. Following are the steps for solving the problem:
- Perform a Post Order Traversal on the given tree and check BST condition for each node where the largest value in the left sub-tree should be smaller than the current value and the smaller value in the right sub tree should be greater the current value.
- Then check if the BST is balanced or not that is the absolute difference between left and right sub-tree should be either 0 or 1.
- Then pass values return from the sub-trees to the parent.
- Perform the above steps for all node and take the boolean variable ans which is initially marked false which is used to check whether a balanced BST is present or not.
- If a Balanced BST of size K is found then print “Yes” else print “No”.
Below is the implementation of above approach:
Time Complexity: O(N)
Auxiliary Space: O(1)
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.
- Convert a normal BST to Balanced BST
- Check if a given Binary Tree is height balanced like a Red-Black Tree
- Check if a Binary Tree contains duplicate subtrees of size 2 or more
- Maximum sub-tree sum in a Binary Tree such that the sub-tree is also a BST
- Check whether BST contains Dead End or not
- A program to check if a binary tree is BST or not
- Check if a Binary Tree (not BST) has duplicate values
- Check if a Binary Tree is BST : Simple and Efficient Approach
- Sorted Array to Balanced BST
- Split a BST into two balanced BSTs based on a value K
- Complexity of different operations in Binary tree, Binary Search Tree and AVL tree
- K'th Largest Element in BST when modification to BST is not allowed
- Check if a binary tree is subtree of another binary tree | Set 1
- Check if a binary tree is subtree of another binary tree | Set 2
- Check whether a binary tree is a full binary tree or not
- Check whether a binary tree is a full binary tree or not | Iterative Approach
- Check whether a given binary tree is skewed binary tree or not?
- Check if a binary tree is subtree of another binary tree using preorder traversal : Iterative
- Check given array of size n can represent BST of n levels or not
- How to determine if a binary tree is height-balanced?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. 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.
Improved By : Akanksha_Rai