Skip to content

Latest commit

 

History

History
75 lines (68 loc) · 2.04 KB

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

File metadata and controls

75 lines (68 loc) · 2.04 KB

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

https://leetcode-cn.com/problems/sum-of-nodes-with-even-valued-grandparent/

Java

/*
 * @Author: Goog Tech
 * @Date: 2020-09-15 10:53:49
 * @LastEditTime: 2020-09-15 10:54:29
 * @Description: https://leetcode-cn.com/problems/sum-of-nodes-with-even-valued-grandparent/
 * @FilePath: \leetcode-googtech\#1315. Sum of Nodes with Even-Valued Grandparent\Solution.java
 * @WebSite: https://algorithm.show/
 */

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */

class Solution {
    // DFS : 深度优先遍历
    public int sumEvenGrandparent(TreeNode root) {
        return dfs(root, -1, -1);
    }
    private int dfs(TreeNode node, int parent, int grandparent) {
        if(node == null) return 0;
        int result = 0;
        if(grandparent % 2 == 0) result += node.val;
        result += dfs(node.left, node.val, parent);
        result += dfs(node.right, node.val, parent);
        return result;
    }
}

Python

'''
Author: Goog Tech
Date: 2020-09-15 10:53:53
LastEditTime: 2020-09-15 10:54:21
Description: https://leetcode-cn.com/problems/sum-of-nodes-with-even-valued-grandparent/
FilePath: \leetcode-googtech\#1315. Sum of Nodes with Even-Valued Grandparent\Solution.py
WebSite: https://algorithm.show/
'''

# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution(object):
    # DFS : 深度优先遍历
    def sumEvenGrandparent(self, root):
        """
        :type root: TreeNode
        :rtype: int
        """
        def dfs(node, parent, grandparent):
            if not node: return 0
            result = 0
            if grandparent % 2 == 0: result += node.val
            result += dfs(node.left, node.val, parent)
            result += dfs(node.right, node.val, parent)
            return result
        return dfs(root, -1, -1)