2017年1月18日 星期三

[LeetCode] 120. Triangle

轉自LeetCode

Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below.
For example, given the following triangle
[
     [2],
    [3,4],
   [6,5,7],
  [4,1,8,3]
]
The minimum path sum from top to bottom is 11 (i.e., 2 + 3 + 5 + 1 = 11).
Note:
Bonus point if you are able to do this using only O(n) extra space, where n is the total number of rows in the triangle.
<Solution>

2017年1月13日 星期五

[LeetCode] 119. Pascal's Triangle II

轉自LeetCode

Given an index k, return the kth row of the Pascal's triangle.
For example, given k = 3,
Return [1,3,3,1].
Note:
Could you optimize your algorithm to use only O(k) extra space?
<Solution>

2017年1月12日 星期四

[LeetCode] 118. Pascal's Triangle

轉自LeetCode

Given numRows, generate the first numRows of Pascal's triangle.
For example, given numRows = 5,
Return
[
     [1],
    [1,1],
   [1,2,1],
  [1,3,3,1],
 [1,4,6,4,1]
]
<Solution>

[LeetCode] 117. Populating Next Right Pointers in Each Node II

轉自LeetCode

Follow up for problem "Populating Next Right Pointers in Each Node".
What if the given tree could be any binary tree? Would your previous solution still work?
Note:
  • You may only use constant extra space.
For example,
Given the following binary tree,
         1
       /  \
      2    3
     / \    \
    4   5    7
After calling your function, the tree should look like:
         1 -> NULL
       /  \
      2 -> 3 -> NULL
     / \    \
    4-> 5 -> 7 -> NULL
<Solution>

2017年1月10日 星期二

[LeetCode] 116. Populating Next Right Pointers in Each Node

轉自LeetCode

Given a binary tree
    struct TreeLinkNode {
      TreeLinkNode *left;
      TreeLinkNode *right;
      TreeLinkNode *next;
    }
Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL.
Initially, all next pointers are set to NULL.
Note:
  • You may only use constant extra space.
  • You may assume that it is a perfect binary tree (ie, all leaves are at the same level, and every parent has two children).
For example,
Given the following perfect binary tree,
         1
       /  \
      2    3
     / \  / \
    4  5  6  7
After calling your function, the tree should look like:
         1 -> NULL
       /  \
      2 -> 3 -> NULL
     / \  / \
    4->5->6->7 -> NULL
<Solution>

2017年1月8日 星期日

[LeetCode] 114. Flatten Binary Tree to Linked List

轉自LeetCode

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
Hints:
If you notice carefully in the flattened tree, each node's right child points to the next node of a pre-order traversal.
<Solution>

2017年1月6日 星期五

[LeetCode] 113. Path Sum II

轉自LeetCode

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>

2017年1月4日 星期三

[LeetCode] 112. Path Sum

轉自LeetCode

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>

[LeetCode] 111. Minimum Depth of Binary Tree

轉自LeetCode

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>

[LeetCode] 110. Balanced Binary Tree

轉自LeetCode

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>

2017年1月3日 星期二

[LeetCode] 109. Convert Sorted List to Binary Search Tree

轉自LeetCode

Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.
<Solution>

[C++] tuple Usage

在 c++11,tuple 變成一個內建的 data structure

這邊記錄一下一些 sample code

<Example 1> Basic usage

2017年1月2日 星期一

2017年1月1日 星期日

[LeetCode] 106. Construct Binary Tree from Inorder and Postorder Traversal

轉自LeetCode

Given inorder and postorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
<Solution>