Given a binary tree, return its diagonal traversal.
The root is on diagonal 0. A right child is on the same diagonal as its parent. A left child is on the next diagonal, one more than its parent.
Return the values of diagonal 0 first, then diagonal 1, and so on. Inside one diagonal, list the nodes in the order a preorder traversal visits them: node first, then its left subtree, then its right subtree.
List<Integer> diagonalTraversal(List<String> tree)
tree.get(i) is node i, written as "value,leftIndex,rightIndex".leftIndex and rightIndex are the indexes of the children in tree, or -1 if the child is missing.0.1 ≤ tree.size() ≤ 100,000-1,000,000,000 ≤ value ≤ 1,000,000,000-1 ≤ leftIndex, rightIndex < tree.size()0. Every other node is the child of exactly one node, and there are no cycles.Input: diagonalTraversal(tree = ["7,1,2", "3,-1,3", "15,-1,4", "6,5,-1", "20,6,7", "4,-1,-1", "18,-1,-1", "25,-1,-1"])
Output: [7,15,20,25,3,6,18,4]
Explanation: Diagonal 0 is the root and its chain of right children: 7, 15, 20, 25. Diagonal 1 holds the left children of diagonal 0 and their right children: 3 and its right child 6, then 18. Diagonal 2 is 4, the left child of 6.
Input: diagonalTraversal(tree = ["8,1,2", "3,-1,3", "12,4,-1", "9,-1,5", "5,6,-1", "1,7,-1", "2,-1,-1", "7,-1,-1"])
Output: [8,12,3,9,1,5,7,2]
Explanation: Diagonal 0 is 8, 12. Diagonal 1 is 3, 9, 1, 5. Node 5 is higher in the tree than node 1, but 1 is in the root's left subtree, so preorder visits it first. For the same reason, diagonal 2 is 7, 2.
Input: diagonalTraversal(tree = ["4,1,-1", "4,2,-1", "-6,-1,-1"])
Output: [4,4,-6]
Explanation: Every child is a left child, so each diagonal has one node. Both nodes with value 4 are included.
Input: diagonalTraversal(tree = ["10,-1,1", "30,-1,2", "20,-1,-1"])
Output: [10,30,20]
Explanation: Every child is a right child, so all nodes are on diagonal 0, from the root down.