VMWare Inc Interview Question for Member Technical Staffs


Country: India
Interview Type: In-Person




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

private static Integer sumAllExcludingLeaves(BinaryTreeNode root) {
		int sum = 0;
		BinaryTreeNode temp;
		Queue<BinaryTreeNode> q = new LinkedBlockingDeque<BinaryTreeNode>();
		q.add(root);
		while (!q.isEmpty()) {
			temp = q.poll();
			if((temp.getLeft()!=null && temp.getRight()==null) || (temp.getLeft()==null && temp.getRight()!=null) || (temp.getLeft()!=null && temp.getRight()!=null))
				sum += temp.getData();
			if (temp.getLeft() != null)
				q.add(temp.getLeft());
			if (temp.getRight() != null)
				q.add(temp.getRight());
		}
		return sum;
	}

- Vir Pratap Uttam May 04, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
7
of 7 vote

int getSum(Tree root)
{
if(root==null) return 0;
if( (root.left==null) && (root.right==null)) return 0;
return root.value+getSum(root.left)+getSum(root.right);
}

- Jackie June 02, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

int getSum(Tree root)
{
if(root==null) return 0;
if( (root.left==null) && (root.right==null)) return root.data;
return root.value+getSum(root.left)+getSum(root.right);
}

if((root.left==null)&&(root.right==null))
//it should return root.data , instead of 0

- Raj March 16, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

it's correct..it should return 0 as we are excluding leaf nodes.

- mgatuiet September 20, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static int sumFromNodesExcludingLeaves(BNode node, int sum) {
		if (node == null) {
			return 0;
		}
		
		if (node.left == null && node.right == null) {
			return 0;
		}
		
		sumFromNodesExcludingLeaves(node.left, sum);
		sumFromNodesExcludingLeaves(node.right, sum);
		
		sum = sum + node.value;
		
		return sum;
		
	}	// sumFromNodesExcludingLeaves()

- muks June 01, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

You can do this without using the external variable 'sum'.

- Ashish June 02, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Using any one traverse method (pre-order, in-order, post-order) of binary tree,
sum the non-left nodes' values.
Below is my code which using pre-order.

#include <cstdio>
#include <iostream>
#include <cstring>
#include <stack>

using namespace std;

struct binaryTreeNode
{
    binaryTreeNode* left;
    binaryTreeNode* right;
    int data;
};

void createBinaryTree(binaryTreeNode*& root)
{
    int itmp = 0;
    cin >> itmp;
    if(itmp > 0)
    {
        if(root == NULL)
        {
            root = new binaryTreeNode();
        }
        root->data = itmp;
        root->left = 0;
        root->right = 0;
        createBinaryTree(root->left);
        createBinaryTree(root->right);
    }
}

int sumBinaryTreeNodeValue(binaryTreeNode* root)
{
    int sum = 0;
    binaryTreeNode* top = 0;
    stack<binaryTreeNode*> bstack;

    bstack.push(root);

    while(!bstack.empty())
    {
        top = bstack.top();
        bstack.pop();
        if(top->left || top->right)
        {
            sum += top->data;
        }
        if(top->right)
        {
            bstack.push(top->right);
        }
        if(top->left)
        {
            bstack.push(top->left);
        }
    }
    return sum;
}

void destroyBinaryTree(binaryTreeNode*& root)
{
    if(root)
    {
        if(root->left)
        {
            destroyBinaryTree(root->left);
        }
        if(root->right)
        {
            destroyBinaryTree(root->right);
        }
        delete root;
    }
}

int main()
{
    binaryTreeNode* root = 0;

    createBinaryTree(root);

    cout << sumBinaryTreeNodeValue(root) << endl;

    destroyBinaryTree(root);

    return 0;
}

- Gabriel June 02, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int sumInternal(Node* curr){
	if(!curr || (!curr->left && !curr->right)) 
		return 0;

	else	
		return(curr->data + sumInternal(curr->left) + 
		sumInternal(curr->right));
}

- Ashish June 02, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class Node {
	int value;
	Node leftChild;
	Node rightChild;
}

public static int getSumExceptLeafNodes(Node currNode) {
	if(currNode == null) return 0;
	else if(currNode.leftChild == null && currNode.rightChild == null)
		return 0;
	else {
		int leftTreeSum = getSumExceptLeafNodes(currNode.leftChild);
		int rightTreeSum = getSumExceptLeafNodes(currNode.rightChild);
		int result = currNode.value + leftTreeSum  + rightTreeSum ;
		return result;
	}
}

System.out.println(getSumExceptLeafNodes(rootNode));

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

The below function solves the problem..

public static int getSumNoLeaf(BTNode root){
              int sum = 0;
              BTNode temp = root;
              Stack<BTNode> s = new Stack<BTNode>();
              s.push(root);
              while(temp != null){

                     if(s.isEmpty())
                           break;
                     temp = s.pop();
                     if(temp.getLeft()!= null || temp.getRight()!=null)
                           sum = sum+ temp.getData();
                     if(temp.getLeft()!= null){
                           s.push(temp.getLeft());
                     if(temp.getRight()!= null)
                           s.push(temp.getRight());
                     }
              }
              return sum;
       }

- Rahul Khanna June 03, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

{

public class BinaryTreeTest {
int sum=0;

public static void main(String[] args) {
new BinaryTreeTest().run();
}

static class Node {
Node left;

Node right;

int value;

public Node(int value) {
this.value = value;
}
}

public void run() {
// build the simple tree from chapter 11.
Node root = new Node(5);
System.out.println("Binary Tree Example");
System.out.println("Building tree with root value " + root.value);
insert(root, 1);
insert(root, 8);
insert(root, 6);
insert(root, 3);
insert(root, 9);
System.out.println("Traversing tree in order");
printInOrder(root);
System.out.println("Traversing tree front-to-back from location 7");
printFrontToBack(root, 7);
}

public void insert(Node node, int value) {
if (value < node.value) {
if (node.left != null) {
insert(node.left, value);
} else {
System.out.println(" Inserted " + value + " to left of "
+ node.value);
node.left = new Node(value);
}
} else if (value > node.value) {
if (node.right != null) {
insert(node.right, value);
} else {
System.out.println(" Inserted " + value + " to right of "
+ node.value);
node.right = new Node(value);
}
}
}

public void printInOrder(Node node) {
if (node != null) {
if(node.left!= null || node.right!=null){
sum+=node.value;
}
printInOrder(node.left);
System.out.println(" Traversed " + node.value);
printInOrder(node.right);
}

System.out.println("====="+sum);
}

/**
* uses in-order traversal when the origin is less than the node's value
*
* uses reverse-order traversal when the origin is greater than the node's
* order
*/
public void printFrontToBack(Node node, int camera) {
if (node == null)
return;
if (node.value > camera) {
// print in order
printFrontToBack(node.left, camera);
System.out.println(" Traversed " + node.value);
printFrontToBack(node.right, camera);
} else if (node.value < camera) {
// print reverse order
printFrontToBack(node.right, camera);
System.out.println(" Traversed " + node.value);
printFrontToBack(node.left, camera);
} else {
// order doesn't matter
printFrontToBack(node.left, camera);
printFrontToBack(node.right, camera);
}
}

}
}

- arun November 13, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public int getCount(Node node){
	  

	if(node != null) {
		 return ((getCount(node.left)) +(getCount(node.right)))+node.value;
	}else{
		return 0;
	}

}

- Anonymous November 08, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public int getCount(Node node){
	  

	if(node != null) {
		 return ((getCount(node.left)) +(getCount(node.right)))+node.value;
	}else{
		return 0;
	}

}

- bina November 08, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public int getCount(Node node){
	  

	if(node != null) {
		 return ((getCount(node.left)) +(getCount(node.right)))+node.value;
	}else{
		return 0;
	}

}

- nn November 08, 2016 | Flag Reply


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