Press "Enter" to skip to content

Huahua's Tech Road

花花酱 LeetCode 102. Binary Tree Level Order Traversal

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

return its level order traversal as:

Solution 1: BFS O(n)

Solution 2: DFS O(n)

 

花花酱 LeetCode 241. Different Ways to Add Parentheses

Given a string of numbers and operators, return all possible results from computing all the different possible ways to group numbers and operators. The valid operators are +- and *.

Example 1:

Input: "2-1-1".

Output: [0, 2]

Example 2:

Input: "2*3-4*5"

Output: [-34, -14, -10, -10, 10]

Solution:

Running time: 3ms

C++

Python3

花花酱 LeetCode 21: Merge Two Sorted Lists

Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.

Solution 1: Iterative O(n)

 

Solution 2: Recursive O(n)

 

花花酱 LeetCode 110. Balanced Binary Tree

Solution 1: O(nlogn)

Solution 2: O(n)

Java

 

Python

 

花花酱 Leetcode 140. Word Break II

Problem

Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, add spaces in s to construct a sentence where each word is a valid dictionary word. You may assume the dictionary does not contain duplicate words.

Return all such possible sentences.

For example, given
s = "catsanddog",
dict = ["cat", "cats", "and", "sand", "dog"].

A solution is ["cats and dog", "cat sand dog"].

UPDATE (2017/1/4):
The wordDict parameter had been changed to a list of strings (instead of a set of strings). Please reload the code definition to get the latest changes.

Solution

Time complexity: O(2^n)

Space complexity: O(2^n)

C++

Python3

 

Related Problems