Tree Problems

HI, can anyone please suggest me like how to solve tree problems in competitive programming .
How they can be implemented and if possible you can just share how did you master this data structure ?

I had the same trouble. All the tree problems that you solve in leetcode or hackerrank, give you the TreeNode as a pointer to a structure. But in cp, implementing that is quite tedious and to be honest, useless.

In cp, I see the given tree as a UAG. (undirected acyclic graph). So we store this tree, just like any other graph. We make an adjacency list as: vector<int> adj[MAXN] or and adjacency matrix as adj[MAXN][MAXN] in C++.

Check this out (Section 5.4, for the different data structures used, and their complexities)
My Submission for a tree problem


Right man you exactly got what I wanted to ask, so i have to first learn graph first and then tree is implemented using that same technique. If thats the case then it two kills in a singe arrow.

1 Like