Twitch Interview Question

For a given binary tree, assign the sibling pointer of each node. A sibling is always the node to its immediate right on the same level of the tree.

Interview Answers

Anonymous

Nov 12, 2015

I dont understand the question

2

Anonymous

Nov 12, 2015

A complete binary tree is a binary, which is completely filled, with the possible exception of the bottom level, which is filled from left and right.

Anonymous

Mar 29, 2016

The correct solution is to do an in-order traversal of the tree. keeping track of the node and the level you're on. Roughly: q.EnQueue(head, level = 1) lastlevel = 0 lastnode = null While (!q.IsEmpty()) node, level = q.DeQueue() if (level == lastlevel) lastnode.right = node lastnode = node lastlevel = level q.EnQueue(node.left, level + 1) q.EnQueue(node.right, level + 1) end while

Anonymous

Nov 8, 2015

Basically approach this using breadth first search. However after you find your next level, keep track of the prev node, and assign prev node's sibling to the current node. Just like assigning next pointers in a linked list.

2