Graph Theory37 sections · 1633 units
Open in Course

Time Complexity of Tree DP

Linear in node count

Each node is visited exactly once during DFS. At each node, you loop through its children to aggregate values. Total work: O(n)O(n) where nn is the number of nodes. Each edge is traversed twice (down and back up), giving O(n)O(n) edge traversals.

Tree DP is fast. No repeated subproblems like in grid DP. The tree structure guarantees linear time. This makes tree DP practical even for large trees with millions of nodes.

Space complexity is O(n)O(n) for the data structures used.