2020年9月13日 星期日

LeetCode 112. Path Sum [Easy] [C++] 解題筆記

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.

Note: A leaf is a node with no children.

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.

2020年9月12日 星期六

LeetCode 111. Minimum Depth of Binary Tree [Easy] [C++] 解題筆記

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.

Note: A leaf is a node with no children.

Example:

Given binary tree [3,9,20,null,null,15,7],

    3
   / \
  9  20
    /  \
   15   7

return its minimum depth = 2.

2020年9月9日 星期三

LeetCode 110. Balanced Binary Tree [Easy] [C++] 解題筆記

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 left and right subtrees of every node differ in height by no more than 1.

 

Example 1:

Given the following tree [3,9,20,null,null,15,7]:

    3
   / \
  9  20
    /  \
   15   7

Return true.

Example 2:

Given the following tree [1,2,2,3,3,null,null,4,4]:

       1
      / \
     2   2
    / \
   3   3
  / \
 4   4

Return false.

LeetCode 109. Convert Sorted List to Binary Search Tree [Medium] [C++] 解題筆記

Given the head of a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.

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.

 

Example 1:

Input: head = [-10,-3,0,5,9]
Output: [0,-3,9,-10,null,5]
Explanation: One possible answer is [0,-3,9,-10,null,5], which represents the shown height balanced BST.

Example 2:

Input: head = []
Output: []

Example 3:

Input: head = [0]
Output: [0]

Example 4:

Input: head = [1,3]
Output: [3,1]

 

Constraints:

  • The number of nodes in head is in the range [0, 2 * 104].
  • -10^5 <= Node.val <= 10^5

2020年9月6日 星期日

LeetCode 108. Convert Sorted Array to Binary Search Tree [Easy] [C++] 解題筆記

Given an array where elements are sorted in ascending order, convert it to a height balanced BST.

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.

Example:

Given the sorted array: [-10,-3,0,5,9],

One possible answer is: [0,-3,9,-10,null,5], which represents the following height balanced BST:

      0
     / \
   -3   9
   /   /
 -10  5

LeetCode 107. Binary Tree Level Order Traversal II [Easy] [C++] 解題筆記

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,null,null,15,7],

    3
   / \
  9  20
    /  \
   15   7

return its bottom-up level order traversal as:

[
  [15,7],
  [9,20],
  [3]
]

2020年9月5日 星期六

LeetCode 104. Maximum Depth of Binary Tree [Easy] [C++] 解題筆記

Given a binary tree, find its maximum depth.

The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.

Note: A leaf is a node with no children.

Example:

Given binary tree [3,9,20,null,null,15,7],

    3
   / \
  9  20
    /  \
   15   7

return its depth = 3.

LeetCode 103. Binary Tree Zigzag Level Order Traversal [Medium] [C++] 解題筆記

Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between).

For example:
Given binary tree [3,9,20,null,null,15,7],

    3
   / \
  9  20
    /  \
   15   7

return its zigzag level order traversal as:

[
  [3],
  [20,9],
  [15,7]
]

2020年8月31日 星期一

LeetCode 102. Binary Tree Level Order Traversal [Medium] [C++] 解題筆記

Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).

For example:
Given binary tree [3,9,20,null,null,15,7],

    3
   / \
  9  20
    /  \
   15   7

return its level order traversal as:

[
  [3],
  [9,20],
  [15,7]
]

2020年8月25日 星期二

LeetCode 101. Symmetric Tree [Easy] [C++] 解題筆記

Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).

For example, this binary tree [1,2,2,3,4,4,3] is symmetric:

     1
    / \
  2   2
 / \    / \
3  4 4  3

 

But the following [1,2,2,null,3,null,3] is not:

    1
    / \
  2   2
   \    \
   3    3

2020年7月19日 星期日

LeetCode 92. Reverse Linked List II [Medium] [C++] 解題筆記

Reverse a linked list from position m to n. Do it in one-pass.

Note: 1 ≤ mn ≤ length of list.

EX:

        Input: 1->2->3->4->5->NULL, m = 2, n = 4

        Output: 1->4->3->2->5->NULL

2020年7月18日 星期六

LeetCode 53. Maximum Subarray [Easy] [C++] 解題筆記

Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.

EX:

        Input: [-2,1,-3,4,-1,2,1,-5,4],

        Output: 6
Explanation: [4,-1,2,1] has the largest sum = 6.

LeetCode 52. N-Queens II [Hard] [C++] 解題筆記

The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other.

Given an integer n, return the number of distinct solutions to the n-queens puzzle.

EX:

     Input: 4

    Output: 2
    Explanation: There are two distinct solutions to the 4-queens puzzle as shown below.
    [
     [".Q..",  // Solution 1
      "...Q",
      "Q...",
      "..Q."],

     ["..Q.",  // Solution 2
      "Q...",
      "...Q",
      ".Q.."]
    ]