root of a binary tree. Add up the values of every node whose grandparent (the parent of its parent) exists and holds an even number.
If no node qualifies, return 0.root=tree_node([4, 3, 5, 1, 9, None, 7]) → 17
The root 4 is even and its grandchildren are 1, 9 and 7: 1 + 9 + 7 = 17. No other node has grandchildren.
root=tree_node([3, 6, None, 2, 8, 5]) → 5
The root 3 is odd, so its grandchildren 2 and 8 do not count. The even node 6 has one grandchild, 5, so the answer is 5.
root=tree_node([10, 20]) → 0
The tree has only two levels, so nobody has a grandchild.
Node.val ≤ 100root=tree_node([4, 3, 5, 1, 9, None, 7])
17
Run checks the first 3 tests in your browser. Submit checks all 78 tests on the server, hidden ones included.