列表

详情


1315. 祖父节点值为偶数的节点和

给你一棵二叉树,请你返回满足以下条件的所有节点的值之和:

如果不存在祖父节点值为偶数的节点,那么返回 0

 

示例:

输入:root = [6,7,8,2,7,1,3,9,null,1,4,null,null,null,5]
输出:18
解释:图中红色节点的祖父节点的值为偶数,蓝色节点为这些红色节点的祖父节点。

 

提示:

原站题解

去查看

上次编辑到这里,代码来自缓存 点击恢复默认模板
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} * }; */ class Solution { public: int sumEvenGrandparent(TreeNode* root) { } };

python3 解法, 执行用时: 92 ms, 内存消耗: 19 MB, 提交时间: 2022-11-15 10:26:01

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def sumEvenGrandparent(self, root: TreeNode) -> int:
        '''
        找到所有值为偶数的节点,并对这些节点的孙子节点(即子节点的子节点)统计答案
        '''
        q = collections.deque([root])
        ans = 0
        while len(q) > 0:
            node = q.popleft()
            if node.val % 2 == 0:
                if node.left:
                    if node.left.left:
                        ans += node.left.left.val
                    if node.left.right:
                        ans += node.left.right.val
                if node.right:
                    if node.right.left:
                        ans += node.right.left.val
                    if node.right.right:
                        ans += node.right.right.val
            if node.left:
                q.append(node.left)
            if node.right:
                q.append(node.right)
        return ans

python3 解法, 执行用时: 96 ms, 内存消耗: 19.2 MB, 提交时间: 2022-11-15 10:24:02

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def sumEvenGrandparent(self, root: TreeNode) -> int:
        ans = 0

        def dfs(gp_val: int, p_val: int, node: TreeNode) -> None:
            '''
            gp_val: 祖父节点的值
            p_val: 父节点的值
            node: 当前节点
            '''
            if not node:
                return
            if gp_val % 2 == 0:
                nonlocal ans
                ans += node.val
            dfs(p_val, node.val, node.left)
            dfs(p_val, node.val, node.right)
        
        dfs(1, 1, root)
        return ans

python3 解法, 执行用时: 96 ms, 内存消耗: 19.3 MB, 提交时间: 2022-11-15 10:22:08

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def sumEvenGrandparent(self, root: TreeNode) -> int:
        ans = 0

        def dfs(grandparent, parent, node):
            if not node:
                return
            if grandparent.val % 2 == 0:
                nonlocal ans
                ans += node.val
            dfs(parent, node, node.left)
            dfs(parent, node, node.right)
        
        if root.left:
            dfs(root, root.left, root.left.left)
            dfs(root, root.left, root.left.right)
        if root.right:
            dfs(root, root.right, root.right.left)
            dfs(root, root.right, root.right.right)
        
        return ans

上一题