Facebook

  www.facebook.com
Work in HR? Unlock Free Profile

Facebook Software Engineer Intern Interview Question (student candidate)

"- Convert sorted array to BST - Print the above tree level by level - I forgot the last question"
Add Tags [?]
Answer

Part of a Software Engineer Intern Interview Review - one of 1,089 Facebook Interview Reviews

Answers & Comments

1
of 1
vote

BinaryTree* sortedArrayToBST(int arr[], int start, int end) {
  if (start > end) return NULL;
  // same as (start+end)/2, avoids overflow.
  int mid = start + (end - start) / 2;
  BinaryTree *node = new BinaryTree(arr[mid]);
  node->left = sortedArrayToBST(arr, start, mid-1);
  node->right = sortedArrayToBST(arr, mid+1, end);
  return node;
}

BinaryTree* sortedArrayToBST(int arr[], int n) {
  return sortedArrayToBST(arr, 0, n-1);
}

- thy on Nov 17, 2013

To comment on this question, Sign In with Facebook or Sign Up

Tags are like keywords that help categorize interview questions that have something in common.