Amazon Interview Question for SDE1s


Country: India
Interview Type: Written Test




Comment hidden because of low score. Click to expand.
6
of 6 vote

Hi,

Here is a simpler and most elegant implementation of the algorithm in Java.

public static int sumTheTree(BSTNode root) {		
		if (root == null) {
			return 0;
		}		
		root.data = root.data + sumTheTree(root.left) + sumTheTree(root.right);		
		return root.data;

}

- Vijay Muvva April 27, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

1                               28 
    2	     3	                   11	       16 
  4  5   6  7               4    5    6    7

node old value --- new value
1 -- 28
2 -- 11
3 -- 16
4 -- 4, 5 -- 5, 6 -- 6, 7 -- 7

- phaniteja.24 April 27, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Traverse the tree in postorder and pass on the sums from child nodes to its parent.

Code:

public int getSumOfChildNodes(BinaryTree root) {
		if (root == null)
			return 0;
		int left = getSumOfChildNodes(root.left);
		int right = getSumOfChildNodes(root.right);
		int sum = left + right + root.data;
		System.out.println(root.data + " " + sum);
		return sum;
	}

- Dhass April 27, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Do a post order traversal and node->data = node->data + node->left->data + node->right->data

int sumTreeNode(Tree * t)
{
if(!t)
return 0;

int leftsum = sumTreeNode(t->left);
int rightsum = sumTreeNode(t->right);

t->data = leftsum + rightsum;
return t->data;
}

- Anonymous April 27, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void createSumTree(struct node *root){
     if(root==NULL || ( root->left==NULL && root->right==NULL)){
                   return;
     }
     else{
          int ls = 0;
          int rs = 0;
          createSumTree(root->left);
          createSumTree(root->right);
          if(root->left){
                 ls = root->left->data;
          }
          if(root->right){
                 rs = root->right->data;         
          }
          root->data = ls + rs + root->data;
     }
}

- Amit April 27, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote
{{{ typedef struct bst { int data; bstptr left; bstptr right; }* bstptr; int treesum(bstptr b) { if(!b) return 0; return b.data + treesum(b->left) + treesum(b->right); } - lyf.pboy April 27, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

typedef struct bst {
	int data;
	bstptr left;
	bstptr right;
}* bstptr;
int treesum(bstptr b) {
	if(!b)
		return 0;
	return b.data + treesum(b->left) + treesum(b->right);
}

- lyf.pboy April 27, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

typedef struct bst {
	int data;
	bstptr left;
	bstptr right;
}* bstptr;
int tree_sum_tree(bstptr b) {
	if(!b)
		return 0;
	b.data = b.data + treesum(b->left) + treesum(b->right);
	return b.data;
}

- lyf.pboy April 29, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

int CalculateSum_Node_ItsChildren(BinaryTree *T)
{
	int v,v1,v2;
	
	if(T == NULL)
	{
		return 0;
	}
	
	v1 = CalculateSum(T->leftChild);
	v2 = CalculateSum(T->rightChild);
	v = T->key + v1+v2;
	
	printf("%d --- %d\n", T->key, v);
	return v;
}

- KTN Varma April 28, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class TreeSumNodes {

    /**
     * @param args
     */
    public static void main(String[] args) {
        TreeNode head = new TreeNode(1);
        head.leftchild = new TreeNode(2);
        head.rightchild = new TreeNode(3);
        head.leftchild.leftchild = new TreeNode(4);
        head.leftchild.rightchild = new TreeNode(5);
        head.rightchild.leftchild = new TreeNode(6);
        head.rightchild.rightchild = new TreeNode(7);

        int sum = sumOfNodePlusChildNodes(head);
        System.out.println(sum);

    }

    private static int sumOfNodePlusChildNodes(TreeNode head) {
        if (head == null) {
            return 0;
        }
        return head.data + sumOfNodePlusChildNodes(head.leftchild) + sumOfNodePlusChildNodes(head.rightchild);
    }

    public static class TreeNode {

        TreeNode leftchild;

        TreeNode rightchild;

        int data;

        public TreeNode(int data) {
            this.data = data;
        }
    }

}

- rams April 28, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static int sumNodesBinaryTree(BTNode currNode) {
	if(currNode== null) return 0;
	else if(currNode.left == null and currNode.right == null)
		return currNode.value;
	else {
		leftChildSum = sumNodesBinaryTree(currNode.left);
		rightChildSum = sumNodesBinaryTree(currNode.right);
		currNode.value += leftChildSum  + rightChildSum ;
		return currNode.value ;
	}

}

- Subhajit April 29, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Correct one:

typedef struct bst {
	int data;
	bstptr left;
	bstptr right;
}* bstptr;

int tree_sum_tree(bstptr b) {
	if(!b)
		return 0;
	b->data = b->data + tree_sum_tree(b->left) + tree_sum_tree(b->right);
	return b->data;
}

- lyf.pboy April 29, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

void SumAtRoot(stTree* &pRoot)
{
if(pRoot == NULL)
return;

SumAtRoot(pRoot->left);
SumAtRoot(pRoot->right);

if(pRoot->left != NULL)
pRoot->data += pRoot->left->data ;
if(pRoot->right != NULL)
pRoot->data += pRoot->right->data ;
}

- Rishi April 29, 2013 | Flag


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More