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.
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.
- 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:
1 5 5
- Minimum and maximum node that lies in the path connecting two nodes in a Binary Tree
- XOR of path between any two nodes in a Binary Tree
- Print path between any two nodes in a Binary Tree
- Print path between any two nodes in a Binary Tree | Set 2
- Shortest path between two nodes in array like representation of binary tree
- Print path from root to all nodes in a Complete Binary Tree
- Maximum Path Sum in a Binary Tree
- Maximum XOR path of a Binary Tree
- Minimum Cost of Simple Path between two nodes in a Directed and Weighted Graph
- LCA in a tree using Binary Lifting Technique
- Find the maximum path sum between two leaves of a binary tree
- Minimum difference between any two weighted nodes in Sum Tree of the given Tree
- Kth ancestor of a node in an N-ary tree using Binary Lifting Technique
- Maximum Consecutive Increasing Path Length in Binary Tree
- Find the maximum sum leaf to root path in a Binary Tree
- Sum of nodes at maximum depth of a Binary Tree
- Maximum sum of nodes in Binary tree such that no two are adjacent
- Sum of nodes at maximum depth of a Binary Tree | Set 2
- Find maximum among all right nodes in Binary Tree
- Maximum sum of leaf nodes among all levels of the given 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 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.