# Find the maximum Even Digit Sum node in the given tree

Given a** tree** with the weights of all the nodes, the task is to find the maximum weighing node whose weight has even digit sum.

**Examples:**

Input:Tree = 5 / \ 10 6 / \ 11 8Output:11Explanation:The tree node weights are: 5 -> 5 10 -> 1 + 0 = 1 6 -> 6 11 -> 1 + 1 = 2 8 -> 8 Here, digit sum for nodes containing 11, 6 and 8 are even. Hence, the maximum weighing even digit sum node is 11.Input:Tree = 1 / \ 4 7 / \ / \ 11 3 15 8Output:15Explanation:Here, digit sum for nodes containing 4, 11, 15 and 8 are even. Hence, the maximum weighing even digit sum node is 15.

**Approach:** To solve the problem mentioned above follow the steps given below:

- The idea is to perform a depth first search on the tree and for every node.
- First find the digit sum for the weight present in the node by iterating through each digit and then check if the node has even digit sum or not.
- Finally, if it has even digit sum then check if this node is the maximum even digit sum node we have found so far, if yes, then make this node the maximum even digit sum node.

Below is the implementation of the above approach:

## C++

`// C++ program to find the ` `// maximum weighing node ` `// in the tree whose weight ` `// has an Even Digit Sum ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `const` `int` `sz = 1e5; ` `int` `ans = -1; ` ` ` `vector<` `int` `> graph[100]; ` `vector<` `int` `> weight(100); ` ` ` `// Function to find the digit sum ` `// for a number ` `int` `digitSum(` `int` `num) ` `{ ` ` ` `int` `sum = 0; ` ` ` `while` `(num) { ` ` ` `sum += (num % 10); ` ` ` `num /= 10; ` ` ` `} ` ` ` ` ` `return` `sum; ` `} ` ` ` `// Function to perform dfs ` `void` `dfs(` `int` `node, ` `int` `parent) ` `{ ` ` ` `// Check if the digit sum ` ` ` `// of the node weight ` ` ` `// is even or not ` ` ` `if` `(!(digitSum(weight[node]) & 1)) ` ` ` `ans = max(ans, weight[node]); ` ` ` ` ` `// Performing DFS to iterate the ` ` ` `// remaining nodes ` ` ` `for` `(` `int` `to : graph[node]) { ` ` ` `if` `(to == parent) ` ` ` `continue` `; ` ` ` `dfs(to, node); ` ` ` `} ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `// Weights of the node ` ` ` `weight[1] = 5; ` ` ` `weight[2] = 10; ` ` ` `weight[3] = 11; ` ` ` `weight[4] = 8; ` ` ` `weight[5] = 6; ` ` ` ` ` `// Edges of the tree ` ` ` `graph[1].push_back(2); ` ` ` `graph[2].push_back(3); ` ` ` `graph[2].push_back(4); ` ` ` `graph[1].push_back(5); ` ` ` ` ` `// Call the dfs function to ` ` ` `// traverse through the tree ` ` ` `dfs(1, 1); ` ` ` ` ` `cout << ans << endl; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to find the ` `# maximum weighing node ` `# in the tree whose weight ` `# has an Even Digit Sum ` `sz ` `=` `1e5` `ans ` `=` `-` `1` ` ` `graph ` `=` `[[] ` `for` `i ` `in` `range` `(` `100` `)] ` `weight ` `=` `[` `-` `1` `] ` `*` `100` ` ` `# Function to find the digit sum ` `# for a number ` `def` `digitSum(num): ` ` ` `sum` `=` `0` ` ` ` ` `while` `num: ` ` ` `sum` `+` `=` `num ` `%` `10` ` ` `num ` `=` `num ` `/` `/` `10` ` ` ` ` `return` `sum` ` ` `# Function to perform dfs ` `def` `dfs(node, parent): ` ` ` `global` `ans ` ` ` ` ` `# Check if the digit sum ` ` ` `# of the node weight ` ` ` `# is even or not ` ` ` `if` `not` `(digitSum(weight[node]) & ` `1` `): ` ` ` `ans ` `=` `max` `(ans, weight[node]) ` ` ` ` ` `# Performing DFS to iterate the ` ` ` `# remaining nodes ` ` ` `for` `to ` `in` `graph[node]: ` ` ` `if` `to ` `=` `=` `parent: ` ` ` `continue` ` ` ` ` `dfs(to, node) ` ` ` `# Driver code ` `def` `main(): ` ` ` ` ` `# Weights of the node ` ` ` `weight[` `1` `] ` `=` `5` ` ` `weight[` `2` `] ` `=` `10` ` ` `weight[` `3` `] ` `=` `11` ` ` `weight[` `4` `] ` `=` `8` ` ` `weight[` `5` `] ` `=` `6` ` ` ` ` `# Edges of the tree ` ` ` `graph[` `1` `].append(` `2` `); ` ` ` `graph[` `2` `].append(` `3` `); ` ` ` `graph[` `2` `].append(` `4` `); ` ` ` `graph[` `1` `].append(` `5` `); ` ` ` ` ` `# Call the dfs function to ` ` ` `# traverse through the tree ` ` ` `dfs(` `1` `, ` `1` `) ` ` ` ` ` `print` `(ans) ` `main() ` ` ` `# This code is contributed by stutipathak31jan ` |

*chevron_right*

*filter_none*

**Output:**

11

**Time Complexity:** O(N)

**Auxiliary Space complexity:** O(1)

## Recommended Posts:

- Find the node with maximum value in a Binary Search Tree
- Find the maximum node at a given level in a binary tree
- Find the node with maximum value in a Binary Search Tree using recursion
- Find farthest node from each node in Tree
- Node having maximum sum of immediate children and itself in n-ary tree
- Maximum sum possible for every node by including it in a segment of N-Ary Tree
- Get maximum left node in binary tree
- Maximum XOR with given value in the path from root to given node in the tree
- Maximum difference between node and its ancestor in Binary Tree
- Minimum valued node having maximum depth in an N-ary Tree
- Range Queries to find the Element having Maximum Digit Sum
- Iterative Segment Tree (Range Maximum Query with Node Update)
- Minimum and maximum node that lies in the path connecting two nodes in a Binary Tree
- Find the node at the centre of an N-ary tree
- Find the node whose xor with x gives maximum value
- Find the Deepest Node in a Binary Tree
- Find root of the tree where children id sum for every node is given
- Find the Kth node in the DFS traversal of a given subtree in a Tree
- Find GCD of each subtree of a given node in an N-ary Tree for Q queries
- Find the parent of a node in the given binary tree

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.