Frage im Vorstellungsgespräch bei Amazon

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

Antworten zu Vorstellungsgespräch

Anonym

1. Mai 2012

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

5

Anonym

22. Juni 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; }

Anonym

1. Feb. 2012

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