2014年4月23日 星期三

[LeetCode] Path Sum II

Problem:
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum.
For example:
Given the below binary tree and sum = 22,
              5
             / \
            4   8
           /   / \
          11  13  4
         /  \    / \
        7    2  5   1
return
[
   [5,4,11,2],
   [5,8,4,5]
]
Solution:O(n)

[LeetCode] Path Sum

Problem:
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.
Solution:O(n)

[LeetCode] Minimum Depth of Binary Tree

Problem:
Given a binary tree, find its minimum depth.
The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.
Solution:O(n)

[LeetCode] Balanced Binary Tree

Problem:
Given a binary tree, determine if it is height-balanced.
For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1.
Solution:O(n)

[LeetCode] Convert Sorted List to Binary Search Tree

Problem:
Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.
Solution1:O(n^2)

[LeetCode] Convert Sorted Array to Binary Search Tree

Problem:
Given an array where elements are sorted in ascending order, convert it to a height balanced BST.
Solution:O(n)

[LeetCode] Binary Tree Level Order Traversal II

Problem:
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root).
For example:
Given binary tree {3,9,20,#,#,15,7},
    3
   / \
  9  20
    /  \
   15   7
return its bottom-up level order traversal as:
[
  [15,7]
  [9,20],
  [3],
]
Solution:O(n)