1315. Sum of Nodes with Even-Valued Grandparent
Medium
Given the root
of a binary tree, return the sum of values of nodes with an even-valued grandparent. If there are no nodes with an even-valued grandparent, return 0
.
A grandparent of a node is the parent of its parent if it exists.
Example 1:
Example 2:
Constraints:
The number of nodes in the tree is in the range
[1, 10^4]
.1 <= Node.val <= 100
解題
透過將 parent、grandparent 節點傳下去的方式判斷是否需要加到 ans
Last updated