Amazon Interview Question
Software Engineer / DevelopersI've seen the remark, "+ve & -ve numbers," in other postings. I'm unfamiliar with this notation. I assume the questioner intends to mean "signed integers." If that's true, why the reference to +ve and -ve? Why not just say signed integers or even just integers which is the more common usage?
Thanks to anyone who responds.
just do the postorder traversal of tree and in each recursive call check weather the parent have same valeue as the sum of its childrean else just do maintain it :)
void BinaryTreeSum(struct node *T)
{
if(!T||T->left==NULL&&T->right==NULL)
return;
BinaryTreeSum(T->left);
BinaryTreeSum(T->right);
if(T->left&&T->right)
sum=T->left->data+T->right->data;
else if(T->left&&!T->right)
sum=T->left->data;
else
sum=T-right->data;
T->data=sum
}
store previous sum of parent node & keep calling for left & right subtree
replace sum of parent node with sum of left + right subtree
#include <stdio.h>
#include <stdlib.h>
/* A binary tree node has data, pointer to left child
and a pointer to right child */
struct node
{
int data;
struct node* left;
struct node* right;
};
/* Helper function that allocates a new node with the
given data and NULL left and right pointers. */
struct node* newNode(int data)
{
struct node* node = (struct node*)
malloc(sizeof(struct node));
node->data = data;
node->left = NULL;
node->right = NULL;
return(node);
}
struct node* sumSubTree(struct node** root)
{
int LeftOld,RightOld;
struct node *l,*r;
if((*root) == NULL || ((*root) -> left== NULL && (*root)->right == NULL))
return NULL;
else
{
LeftOld = (*root)->left->data;
RightOld = (*root)->right->data;
l = sumSubTree(&(*root )->left);
r = sumSubTree(&(*root)->right);
(*root)->data = LeftOld + RightOld;
if(!l)
(*root)->data += l->data;
if(!r)
(*root)->data += r->data;
return *root;
}
}
/* Given a binary tree, print its nodes in inorder*/
void printPreorder(struct node* node)
{
if (node == NULL)
return;
/* first print data of node */
printf("%d ", node->data);
/* then recur on left sutree */
printPreorder(node->left);
/* now recur on right subtree */
printPreorder(node->right);
}
int main()
{
struct node *root = newNode(1);
root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);
root->left->right = newNode(5);
root=sumSubTree(&root);
printf("\n Preorder traversal of binary tree is \n");
printPreorder(root);
getchar();
return 0;
}
<pre lang="" line="1" title="CodeMonkey75851" class="run-this">void ConvertToSumTree(TreeNode* root)
{
if(root != NULL)
{
ConvertToSumTree(root->left);
ConvertToSumTree(root->right);
if(root->left || root->right)
root->data = (root->left ? root->left->data : 0) + (root->right ? root->right->data : 0);
}
}</pre><pre title="CodeMonkey75851" input="yes">
</pre>
public void binarySumTree2(Node localRoot){
if(localRoot.leftChild!=null&&localRoot.rightChild!=null){
localRoot.key=getSum(localRoot.leftChild)+getSum(localRoot.rightChild);
binarySumTree(localRoot.leftChild);
binarySumTree(localRoot.rightChild);
}
if(localRoot.leftChild!=null&&localRoot.rightChild==null){
localRoot.key=getSum(localRoot.leftChild);
binarySumTree(localRoot.leftChild);
}
if(localRoot.leftChild==null&&localRoot.rightChild!=null){
localRoot.key=getSum(localRoot.rightChild);
binarySumTree(localRoot.rightChild);
}
}
public int getSum(Node localRoot){
if(localRoot==null){
return 0;
}
else{
return localRoot.key+getSum(localRoot.leftChild)+getSum(localRoot.rightChild);
}
}
Do simple recursive call to sum up all nodes
- andrew July 31, 2011int ProcessTree(Node *node)
{
if (!node) return 0;
int cur_node_data = node->data;
node->data = ProcessTree(node->left) + ProcessTree(node->right);
return node->data + cur_node_data;
}