Given a Binary Indexed Tree with N nodes except root node 0 (Numbered from 1 to N), Find its diameter.
Binary Indexed Tree is a tree where parent of a node number X = X – (X & (X – 1)) i.e. last bit is unset in X. The diameter of a tree is the longest simple path between any two leaves.
Input: N = 12
Explanation: Path from node 7 to node 11.
Input : n = 15
Output : 7
- In a BIT, root is always node 0. In first level, all nodes are of power of 2 . (1, 2, 4, 8, ….)
- Consider any node in the first level (1, 2, 4, 8, ) its sub-tree will include all the nodes which has same number of bits as that of the root.
- Sub-Tree with root 1 will have no child.
- Sub-Tree with root 2 will have 3 as a child.
- Sub-Tree with root 4 will have 5, 6, 7 as a child.
- Sub-Tree with root 8 will have 9, 10, 11, 12, 13, 14, 15 as a child. (Double the size of the previous subtree)
- So subtree with root K will have K nodes including root. And the height of each subtree would be equal:
- for subtree with root 1
- for subtree with root 2
- for subtree with root 4
- Now, we need to find the subtree in which N lies. Say, the height of subtree just before the subtree in which N lies is H and size is L. So, the following cases are possible :
- Case 1 : When N >= L*2 – 1, in such a scenario N is in last level of its subtree. Thus, the diameter will be 2*H + 1. (Path from the lowest level leaf of the previous subtree to the N ).
- Case 2 : When N >= L + L/2 – 1, in such a scenario N is at level H in its subtree. Thus, diameter will be 2*H.
- Case 3 : Otherwise, it is optimal to consider the maximum path length between leaf nodes of two subtree just before the subtree in which N lies i.e diameter is 2*H – 1.
Below are the implementation of the above approach:
- Diameter of a Binary Tree
- Binary Indexed Tree : Range Updates and Point Queries
- Diameter of a Binary Tree in O(n) [A new method]
- Sum of all nodes in a binary tree
- Sink even nodes in Binary Tree
- Sum of nodes in the right view of the given binary tree
- Sum of all the Boundary Nodes of a Binary Tree
- XOR of path between any two nodes in a Binary Tree
- Sink Odd nodes in Binary Tree
- Sum of all leaf nodes of binary tree
- Sum of nodes in top view of binary tree
- Product of all nodes in a Binary Tree
- Sum of nodes in bottom view of Binary Tree
- Check if two nodes in a Binary Tree are siblings
- Sum of nodes at maximum depth of a Binary Tree
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.