Sum of Magic Nodes

Company: Statestreet_13oct

Difficulty: medium

Problem Statement

Sum of Magic Nodes Problem Description A positive integer is called a Special number if it eventually becomes 1 when it is repeatedly replaced by the sum of the squares of its digits. For example, start from 23 : 2^2 + 3^2 = 4 + 9 = 13 1^2 + 3^2 = 1 + 9 = 10 1^2 + 0^2 = 1 + 0 = 1 The process reaches 1 , so 23 is a Special number. The number 1 itself is Special. A number that never reaches 1 (the process falls into a loop instead, as it does for 4 -> 16 -> 37 -> 58 -> 89 -> 145 -> 42 -> 20 -> 4 ) is not Special. You are given a binary tree of N nodes: one root and N - 1 other nodes. Every node holds a positive integer value. A node is a Magic node if it has both a left child and a right child, and the values of both children are Special numbers. A node with fewer than two children is never a Magic node. Each non-root node is described by its path from the root, a string of the letters L and R : L is the left child of the root, R the right child of the root, LL th