贪心算法  只顾当前最优,贪心算法在很多情况下,是不适用的。生活中的实际问题,不能处处只看眼前。  举例:最少纸币问题  当问题可以拆分成子问题,子问题有最优解时,可以用贪心算法。 这种子问题最优解成为最优子结构。  动态规划就是子问题不一定选择最优解,而是多选几种可能,存起来所有可能的解。贪心算法无法回退,动态规划保存了之前的结果,有回退的功能。 实战 122. 买卖股票的最佳时机 II https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-ii/  广度优先搜索   这个理解为魂斗罗里面的散弹枪  本质上是把根节点和儿子节点依次加入到队列queue中。先进先出。 1先进来,然后1出去,放1的儿子2,3,4.然后2 出去,放2的儿子5,3的儿子6,7,4的儿子8。然后5出去,放5的儿子9。6出去,放6的儿子10.
BFS不需要递归,而是用一个queue来进行。  visited的作用就是判定是否已经访问过了,不然的话,可能会成为环。就是图,而不是二叉树了。
深度优先搜索:广度优先更符合人的思维,深度优先更符合计算机的思维,就是用statck栈。    BFS和DFS比较: BFS地毯式推进,DFS一条路走到黑,不撞南墙不回头。   stack栈是先入后出,这里面的递归就是实现了stack的作用。 DFS非递归写法:不常用,手动实现stack。  实战 102. 二叉树的层序遍历 https://leetcode-cn.com/problems/binary-tree-level-order-traversal/    104. 二叉树的最大深度 https://leetcode-cn.com/problems/maximum-depth-of-binary-tree/    实战 22. 括号生成 https://leetcode-cn.com/problems/generate-parentheses/  
|