tree的题可以考虑:
* BSF/DFS
* DFS: pre-order, in-order, post-order
* BFS: level order traverse
* divide & conquer (无脑看左右子树 然后再考虑最后的结果)
* top down - recursive
* bottom up - iterative
* 给node编号, 第N层可以2^(n-1)个node ..
* 空间优化(知道就行):Morris traversal: iterative approach to leverage preorder traversal that the the entire left subtree would be placed between the node and its right subtree
BST 考虑利用
* inorder traverse是个sorted array
* 左子树 < ROOT < 右子树 (某些可能有等于), 向下DFS的时候,记得更新MIN和MAX
. 1point3acres.com
BFS/DFS traverse
* 求最短或者最少XX 一般是用BFS,比如最短路径
* use BFS for unweighted graphs and use Dijkstra's algorithm for weighted graphs
* dijkstra 和普通图的不同之处在于,每个edge有weight,可以用min/max heap来找下一个node
* An implied condition to apply the Dijkstra's algorithm is that the weights of the graph must be positive. 如果negative,需要用另一个算法,不需要掌握
array
* prefix sum很多时候都会用到,以及prefix sum - target有无出现和出现的次数. From 1point 3acres bbs
* 类似于prefix sum的还有prefix product,但是计算product时,需要注意有0,
* 注意subsequence和subarray的相同和区别之处 . 1point3acres * 相同:不能改变element order,所以都不能sort
* 不同:subsequence可以不连续,subarray必须是连续的一段
* 去重
* 一般是用set或者map
* 或者sort,然后看前后是不是一样,一样的话 就skip
* min/max记得考虑local min/max 和 global min/max 以及与当前num的比较
* 尤其是找相乘的product的时候,negative*negative=positive
* 找MIN的时候 初始化成最大,一般是 float('inf') 或者0 (全是正数)
* 找MAX的时候 初始化成最小,一般是float('-inf') 或者0 (全是正数). check 1point3acres for more.
* min/max 还可以用heap
* Sliding window can only be used when nums are all positive, if it exists negative number, DP is needed
* intervals: sort by start or end, 有无overlap 看是否 current start > last end
* 如果找下一个小的或者大的,考虑能不能用mono stack
* 需要O(1) delete操作的时候,如果不需要保持原有的顺序,可以把最后一个element copy到要删除的地方, 然后删掉最后一个element