Showing posts with label Leetcode. Show all posts
Showing posts with label Leetcode. Show all posts

Monday, 2 September 2013

Unique Binary Search Trees

Problem Statement


Given n, how many structurally unique BST's (binary search trees) that store values 1...n?

For example,
Given n = 3, there are a total of 5 unique BST's.
   1         3     3      2      1
\ / / / \ \
3 2 1 1 3 2
/ / \ \
2 1 2 3

Path Sum in Binary Tree

Problem Statement


Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.

For example:
Given the below binary tree and sum = 22,
              5
/ \
4 8
/ / \
11 13 4
/ \ \
7 2 1

return true, as there exist a root-to-leaf path 5->4->11->2 which sum is 22.

Addition in Linked List

Problem Statement


You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8

Sunday, 1 September 2013

Flatten Binary Tree to Linked List

Problem Statement


Given a binary tree, flatten it to a linked list in-place.

For example,
Given
         1
/ \
2 5
/ \ \
3 4 6

The flattened tree should look like:
   1
\
2
\
3
\
4
\
5
\
6

Saturday, 31 August 2013

Remove nth Node in List

Problem Statement


Given a linked list, remove the nth node from the end of list and return its head