Input : level = [10, 15, 14, 25, 30] Output : True The tree of the given level order traversal is 10 / \ 15 14 / \ 25 30 We see that each parent has a value less than its child, and hence satisfies the min-heap property Input : level = [30, 56, 22, 49, 30, 51, 2, 67] Output : False The tree of the given level order traversal is 30 / \ 56 22 / \ / \ 49 30 51 2 / 67 We observe that at level 0, 30 > 22, and hence min-heap property is not satisfied
We need to check whether each non-leaf node (parent) satisfies the heap property. For this, we check whether each parent (at index i) is smaller than its children (at indices 2*i+1 and 2*i+2, if the parent has two children). If only one child, we only check the parent against index 2*i+1.
These algorithms run with worse case O(n) complexity
This article is contributed by Deepak Srivatsav. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Check if the level order traversal of a Binary Tree results in a palindrome
- Check if the given array can represent Level Order Traversal of Binary Search Tree
- Print a Binary Tree in Vertical Order | Set 3 (Using Level Order Traversal)
- Flatten Binary Tree in order of Level Order Traversal
- Density of Binary Tree using Level Order Traversal
- Perfect Binary Tree Specific Level Order Traversal
- Perfect Binary Tree Specific Level Order Traversal | Set 2
- Deletion of a given node K in a Binary Tree using Level Order Traversal
- Calculate height of Binary Tree using Inorder and Level Order Traversal
- Build Binary Tree from BST such that it's level order traversal prints sorted data
- Check if value exists in level-order sorted complete binary tree
- Level Order Tree Traversal
- Insertion in n-ary tree in given order and Level order traversal
- Zig Zag Level order traversal of a tree using single queue
- General Tree (Each node can have arbitrary number of children) Level Order Traversal