

Preorder list can be divided into root, left tree, right tree. Construct Binary Tree from Preorder and Postorder Traversal.Construct Binary Tree from Inorder and Postorder Traversal.Construct Binary Tree from Preorder and Inorder Traversal.Each child node only has one parent node. There is exactly one path from the root to any other node. empty ( ) ) // then do your operations for this level Here is a very clean way to do this “level” level operation in BFS. (2) For each level, compare unsorted array and sorted array, and count the number of swap operations. Minimum Number of Operations to Sort a Binary Tree by Level.Since it may lead to exponential increase if each element has same branching factors. You can also use bi-directional way to solve BFS. The core is to create the words graph by using underdetermined words with O(N len(word)) instead of the original words with O(NN*len(word). See 2470 below for doing operations for each level after finishing them while maintainign only one queue. O(n) time, O(n) space for one directional BFS, O(n^(0.5)) space for bi-directional BFS. Traver a dict that stores how many times of a number are left to use. You also need a variable to store the last number you have met in the for loop. Traverse the list and use a set to store all the elements you have met. Traver a dict that stores whether a nums have been used. Dealing with the repetitive numbers is a tricky part.

In recursion, you can use a pointer to indicate the head of the list, and conduct a for loop to traverse from the current head to the tail of the list. Convert Sorted Array to Binary Search Tree.If you need to transform a recursion to a postorder traversal for dfs more complex than this problem, you can restore some key variables as a list in each position of the stack.Īssume the root has infinite coins, count how many coin moves the left subtree and right subtree need separately.Ĭut branches when there are only right parentheses. Pace: append,append,…append,pop,append,append,…,append You can count the total depth to judge when it goes through all the elements. You can flag the current node to avoid repeting it in the following recursions. Memorize the previous nodes’ ranges.ĭFS function’s parameter can have depth. DP is too time consuming for it.ĭFS on the tree. list in python).īe careful that you shouldn’t use elif when you want to try all the directions in your dfs function.ĭFS is used by recursion. You can use recursion or iteration(stack, i.e. Using this list is much better than storing all the variations of an anagram. Use a dict to store frequency lists of 26 letters. For this kind of problems, you need to memorize every number you meet, and since they are not bounded in a fixed range, you should use a hashmap to memorize them. You can use hashmap to store all the prefix sum and their frequency, and then search for the current prefix sum - sum. This kind of problem uses prefix sum and hashmap a lot. This is essentialy a k-diff problem, but on prefix sums instead of array elements. So the space complexity is hard to be reduced to O(1). And there is an additional condition of k difference. But the range of numbers is not restricted to 1~n. This question belongs to the duplicate number kind of problem. Since the majority appears more than N/2, you will find it at last. You should use Boyer-Moore Voting Algorithm: you can consider the majority as one type, and the others as the other type, and then use a counter to count the numbers last majority appears. Hash is too space consuming for this problem. Use the index as the value in the dict to avoid the case k=0. Go through the list twice so that the order you meet the two elements in a pair won’t matter. Start from the beginning of each sequence.Īdvanced version of two sum. Use Hash (in python it is set) to quickly judge whether an element is in a set. If it is not in the set, add its mirror item to the set. If it is already in the set, add these three numbers to the final answer, which is a set so we can avoid duplicate ones. To optimize, you can first sort the array, and traverse it one by one for all the negative ones.įor each two-sum problems, you only need to restore one element of the pair in the set. I used weighting and path compression.ĭivide and conquer. List, set, dict, heapq, collections.Counter, que, queue.PriorityQueue, faultdict
