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.
- 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
- How to determine if a binary tree is height-balanced?
- Create Balanced Binary Tree using its Leaf Nodes without using extra space
- Check if a binary tree is subtree of another binary tree using preorder traversal : Iterative
- 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 | Set 2
- Check if a binary tree is subtree of another binary tree | Set 1
- Check whether a binary tree is a full binary tree or not
- Check if max sum level of Binary tree divides tree into two equal sum halves
- Given level order traversal of a Binary Tree, check if the Tree is a Min-Heap
- Check whether a binary tree is a complete tree or not | Set 2 (Recursive Solution)
- Check if the given binary tree has a sub-tree with equal no of 1's and 0's | Set 2
- Check if the given n-ary tree is a binary tree
- Check if a given Binary Tree is Heap
- Check if a given Binary Tree is SumTree
- Check whether a given binary tree is perfect or not
- A program to check if a binary tree is BST or not
- Check if the given Binary Tree have a Subtree with equal no of 1's and 0's
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.
Improved By : Akanksha_Rai