Pergunta de entrevista da empresa Amazon

Coding question - given a binary tree, write code to count the sum off all siblings.

Respostas da entrevista

Sigiloso

1 de mai. de 2012

int totalNumberOfNodes(Node root) { if(root == null) return 0; return 1 + totalNumberOfNodes(root.left) + totalNumberOfNodes(root.right); }

5

Sigiloso

22 de jun. de 2012

private int siblingCounter = 0; public int countSiblings(BinaryNode node) { if(node.element == null) { return siblingCounter; } else { siblingCounter++; if(node.right() != null) { countSiblings(BinaryNode right); } if(node.left() != null) { countSiblings(BinaryNode left); } } return siblingCounter; }

Sigiloso

1 de fev. de 2012

Not sure if this works... value == null) return $sum; $sum = $sum + $tree->value; countSiblings($tree->left,$sum); countSiblings($tree->right,$sum); } ?>