Welcome to Subscribe On Youtube

1315. Sum of Nodes with Even-Valued Grandparent

Description

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:

Input: root = [6,7,8,2,7,1,3,9,null,1,4,null,null,null,5]
Output: 18
Explanation: The red nodes are the nodes with even-value grandparent while the blue nodes are the even-value grandparents.

Example 2:

Input: root = [1]
Output: 0

 

Constraints:

  • The number of nodes in the tree is in the range [1, 104].
  • 1 <= Node.val <= 100

Solutions

DFS.

  • /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode() {}
     *     TreeNode(int val) { this.val = val; }
     *     TreeNode(int val, TreeNode left, TreeNode right) {
     *         this.val = val;
     *         this.left = left;
     *         this.right = right;
     *     }
     * }
     */
    class Solution {
        private int res;
    
        public int sumEvenGrandparent(TreeNode root) {
            res = 0;
            dfs(root, root.left);
            dfs(root, root.right);
            return res;
        }
    
        private void dfs(TreeNode g, TreeNode p) {
            if (p == null) {
                return;
            }
            if (g.val % 2 == 0) {
                if (p.left != null) {
                    res += p.left.val;
                }
                if (p.right != null) {
                    res += p.right.val;
                }
            }
            dfs(p, p.left);
            dfs(p, p.right);
        }
    }
    
  • /**
     * 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 res;
    
        int sumEvenGrandparent(TreeNode* root) {
            res = 0;
            dfs(root, root->left);
            dfs(root, root->right);
            return res;
        }
    
        void dfs(TreeNode* g, TreeNode* p) {
            if (!p) return;
            if (g->val % 2 == 0) {
                if (p->left) res += p->left->val;
                if (p->right) res += p->right->val;
            }
            dfs(p, p->left);
            dfs(p, p->right);
        }
    };
    
  • # 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:
            self.res = 0
    
            def dfs(g, p):
                if p is None:
                    return
                if g.val % 2 == 0:
                    if p.left:
                        self.res += p.left.val
                    if p.right:
                        self.res += p.right.val
                dfs(p, p.left)
                dfs(p, p.right)
    
            dfs(root, root.left)
            dfs(root, root.right)
            return self.res
    
    
  • /**
     * Definition for a binary tree node.
     * type TreeNode struct {
     *     Val int
     *     Left *TreeNode
     *     Right *TreeNode
     * }
     */
    
    var res int
    
    func sumEvenGrandparent(root *TreeNode) int {
    	res = 0
    	dfs(root, root.Left)
    	dfs(root, root.Right)
    	return res
    }
    
    func dfs(g, p *TreeNode) {
    	if p == nil {
    		return
    	}
    	if g.Val%2 == 0 {
    		if p.Left != nil {
    			res += p.Left.Val
    		}
    		if p.Right != nil {
    			res += p.Right.Val
    		}
    	}
    	dfs(p, p.Left)
    	dfs(p, p.Right)
    }
    
  • /**
     * Definition for a binary tree node.
     * class TreeNode {
     *     val: number
     *     left: TreeNode | null
     *     right: TreeNode | null
     *     constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
     *         this.val = (val===undefined ? 0 : val)
     *         this.left = (left===undefined ? null : left)
     *         this.right = (right===undefined ? null : right)
     *     }
     * }
     */
    
    function sumEvenGrandparent(root: TreeNode | null): number {
        const dfs = (root: TreeNode | null, x: number): number => {
            if (!root) {
                return 0;
            }
            const { val, left, right } = root;
            let ans = dfs(left, val) + dfs(right, val);
            if (x % 2 === 0) {
                ans += left?.val ?? 0;
                ans += right?.val ?? 0;
            }
            return ans;
        };
        return dfs(root, 1);
    }
    
    

All Problems

All Solutions