Sum of Nodes with Even-Valued Grandparent

Given a binary tree, return the sum of values of nodes with an even-valued grandparent.  (A grandparent of a node is the parent of its parent if it exists.)
If there are no nodes with an even-valued grandparent, return 0.

Example:

Input:  tree={6,7,8,2,7}
Output: 9

Approach

Java

public class SumNodesEvenValuedGrandparent {
    public static void main(String[] args) {
        TreeNode root = new TreeNode(6);
        root.left = new TreeNode(7);
        root.right = new TreeNode(8);
        root.left.left = new TreeNode(2);
        root.left.right = new TreeNode(7);
        System.out.println(sumEvenGrandparent(root));
    }

    static int res = 0;

    static void dfs(TreeNode root) {
        // if empty tree
        if (root == null)
            return;
        if (root.val % 2 == 0) {
            // if left &&left.left
            if (root.left != null && root.left.left != null)
                res += root.left.left.val;
            // if left &&left .right
            if (root.left != null && root.left.right != null)
                res += root.left.right.val;
            // if right&&right.left
            if (root.right != null && root.right.left != null)
                res += root.right.left.val;
            // if right&&right.right
            if (root.right != null && root.right.right != null)
                res += root.right.right.val;
        }
        // left subtree
        dfs(root.left);
        // right subtree
        dfs(root.right);
    }

    static int sumEvenGrandparent(TreeNode root) {
        res = 0;
        dfs(root);
        return res;
    }
}


class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;

    TreeNode() {
    }

    TreeNode(int val) {
        this.val = val;
    }

    TreeNode(int valTreeNode leftTreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}

C++

#include <bits/stdc++.h>
using namespace std;
//struct for treenode
struct TreeNode{
    int data;
    TreeNode *left;
    TreeNode *right;
    TreeNode(int data)
    {
        this->data=data;
        this->left=NULL;
        this->right=NULL;
    }
}; 


void dfs(TreeNode *root,int &res)
{
        //if  empty tree
        if(root==NULL)
              return;
        if(root->data%2==0)
        {
            //if left &&left->left
            if(root->left!=NULL&&root->left->left!=NULL)
                  res+=root->left->left->data;
            //if left &&left ->right
            if(root->left!=NULL &&root->left->right!=NULL)
                   res+=root->left->right->data;
            //if right&&right->left
            if(root->right!=NULL &&root->right->left!=NULL)
                   res+=root->right->left->data;
            //if right&&right->right
            if(root->right!=NULL&&root->right->right!=NULL)
                   res+=root->right->right->data;
        }
        //left subtree
        dfs(root->left,res);
        //right subtree
        dfs(root->right,res);
}
int sumEvenGrandparent(TreeNode* root
{
       int res=0;
       dfs(root,res);
       return res;
}

int main()
{
    TreeNode *rootnew TreeNode(6);
    root->left=new TreeNode(7);
    root->right=new TreeNode(8);
    root->left->left=new TreeNode(2);
    root->left->right=new TreeNode(7);
    cout<<sumEvenGrandparent(root);
    return 0;
}


No comments:

Post a Comment