We are given a rooted tree $T$ of distinct Natural numbers. The goal is to find the largest subtree of $T$ that has MinHeap property. In fact, we want to calculate the largest subset $S$ of nodes, in a way that all nodes in $S$ have a common ancestor in $S$, and any ancestor of $n \in S$ that appeared in $S$ has a smaller value than $n$.
I can solve this problem by DP in $O(n^2)$ time, but I want to know if there is an algorithm to solve it in $O(n \log n)$ time?
For the $O(n^2)$ solution we can use the $DP$ technique. First of all, we maintain a sorted array of nodes, named $nums[n]$, then we define the array $DP[n][n]$.
$DP[i][j]$ maintains the solution for the subtree rooted by $i$, such that the biggest number in the solution is less than or equal to $nums[j]$. In this manner, we can dynamically full the DP by traversing the tree from bottom to top.