Solution Review: Is Tree a Heap
Explore how to determine if a given tree is a min heap by checking its completeness and the parent node values in relation to its children. Understand two methods that traverse the tree recursively and efficiently, ensuring the heap properties hold. Gain insights on the implementation and time complexity for robust coding in Go.
We'll cover the following...
We'll cover the following...
Solution
To see if the given tree is a heap, we need to check the following conditions:
- It is a complete tree.
- The value of a parent node is smaller than or equal to its left and right child.
We can check these conditions using the following two methods.
First method
The first method is to test if a given tree is complete and if the parent-child property is followed. If a tree is a complete tree and all parent nodes in the tree have a value less than or equal to its children, this tree represents a min heap.
Solution code
Time complexity
The isCompleteTree() ...