VOOZH about

URL: https://www.geeksforgeeks.org/dsa/maximum-weighted-edge-in-path-between-two-nodes-in-an-n-ary-tree-using-binary-lifting/

⇱ Maximum weighted edge in path between two nodes in an N-ary tree using binary lifting - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Maximum weighted edge in path between two nodes in an N-ary tree using binary lifting

Last Updated : 15 Jul, 2025

Given an N-ary tree with weighted edge and Q queries where each query contains two nodes of the tree. The task is to find the maximum weighted edge in the simple path between these two nodes.
Examples: 
 

👁 Image

Naive Approach: A simple solution is to traverse the whole tree for each query and find the path between the two nodes.
Efficient Approach: The idea is to use binary lifting to pre-compute the maximum weighted edge from every node to every other node at distance of some 

. We will store the maximum weighted edge till 

level. 

and 

where 

  • j is the node and
  • i is the distance of
  • dp[i][j] stores the parent of j at
  • distance if present, else it will store 0
  • mx[i][j] stores the maximum edge from node j to the parent of this node at
  • distance.


We'll do a depth-first search to find all the parents at 

distance and their weight and then precompute parents and maximum edges at every 

distance.
Below is the implementation of the above approach:


Output: 
1
5
5

 

Time Complexity: O(N*logN).
Auxiliary Space: O(N*logN).
 

Comment
Article Tags: