Pregunta de entrevista de Amazon

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

Respuestas de entrevistas

Anónimo

1 de may de 2012

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

5

Anónimo

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; }

Anónimo

1 de feb de 2012

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