Leetcode-Question-51: N-Queens


51. N-Queens
Difficulty: Hard

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 all distinct solutions to the n-queens puzzle.

Each solution contains a distinct board configuration of the n-queens’ placement, where ‘Q’ and ‘.’ both indicate a queen and an empty space respectively.


For example,
There exist two distinct solutions to the 4-queens puzzle:



Leetcode-Question-133: Clone Graph



  1. Clone Graph
    Difficulty: Medium
    Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors.

OJ’s undirected graph serialization:
Nodes are labeled uniquely.

We use # as a separator for each node, and , as a separator for node label and each neighbor of the node.
As an example, consider the serialized graph {0,1,2#1,2#2,2}.

The graph has a total of three nodes, and therefore contains three parts as separated by #.

First node is labeled as 0. Connect node 0 to both nodes 1 and 2.
Second node is labeled as 1. Connect node 1 to node 2.
Third node is labeled as 2. Connect node 2 to node 2 (itself), thus forming a self-cycle.

Visually, the graph looks like the following:





Leetcode-Question-337: House Robber III


337. House Robber III
Difficulty: Medium

The thief has found himself a new place for his thievery again. There is only one entrance to this area, called the “root.” Besides the root, each house has one and only one parent house. After a tour, the smart thief realized that “all houses in this place forms a binary tree”. It will automatically contact the police if two directly-linked houses were broken into on the same night.

Determine the maximum amount of money the thief can rob tonight without alerting the police.

Example 1:

Maximum amount of money the thief can rob = 3 + 3 + 1 = 7.
Example 2:

Maximum amount of money the thief can rob = 4 + 5 = 9.


Let f1(node) be the value of maximum money we can rob from the subtree with node as root ( we can rob node if necessary).
f2(node) be the value of maximum money we can rob from the subtree with node as root but without robbing node.
Then we have
f2(node) = f1(node.left) + f1(node.right) and
f1(node) = max( f2(node.left)+f2(node.right)+node.value, f2(node) ).


Leetcode-Question-110: Balanced Binary Tree



  1. Balanced Binary Tree
    Difficulty: Easy

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.


  • 自顶向下方法,时间复杂度为O(n^2)