编程知识 cdmana.com

当代程序员必备技能(算法)之:递归详解

{"type":"doc","content":[{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"前言"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"递归是一种非常重要的算法思想,无论你是前端开发,还是后端开发,都需要掌握它。在日常工作中,统计文件夹大小,解析xml文件等等,都需要用到递归算法。它太基础太重要了,这也是为什么面试的时候,面试官经常让我们手写递归算法。本文呢,将跟大家一起学习递归算法~"}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"什么是递归?"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"递归的特点"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"递归与栈的关系"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"递归应用场景"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"递归解题思路"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"leetcode案例分析"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"递归可能存在的问题以及解决方案"}]}]}]},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"什么是递归?"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"递归,在计算机科学中是指一种通过重复将问题分解为同类的子问题而解决问题的方法。简单来说,递归表现为函数调用函数本身。在知乎看到一个比喻递归的例子,个人觉得非常形象,大家看一下:"}]},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"*"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"递归最恰当的比喻,就是查词典。我们使用的词典,本身就是递归,为了解释一个词,需要使用更多的词。当你查一个词,发现这个词的解释中某个词仍然不懂,于是你开始查这第二个词,可惜,第二个词里仍然有不懂的词,于是查第三个词,这样查下去,直到有一个词的解释是你完全能看懂的,那么递归走到了尽头,然后你开始后退,逐个明白之前查过的每一个词,最终,你明白了最开始那个词的意思。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"*"}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"来试试水,看一个递归的代码例子吧,如下:"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"public int sum(int n) {\n    if (n <= 1) {\n        return 1;\n    } \n    return sum(n - 1) + n; \n}\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"递归的特点"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"实际上,递归有两个显著的特征,终止条件和自身调用:"}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"自身调用:原问题可以分解为子问题,子问题和原问题的求解方法是一致的,即都是调用自身的同一个函数。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"终止条件:递归必须有一个终止的条件,即不能无限循环地调用本身。"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"结合以上demo代码例子,看下递归的特点:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/ed/ed0df7a912bda93939cec59b575875d4.png","alt":"程序员必备的基本算法:递归详解","title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"递归与栈的关系"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"其实,递归的过程,可以理解为出入栈的过程的,这个比喻呢,只是为了方便读者朋友更好理解递归哈。以上代码例子计算sum(n=3)的出入栈图如下:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/6e/6e9d053109ebbf4157ec21cdab40293d.jpeg","alt":"程序员必备的基本算法:递归详解","title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"为了更容易理解一些,我们来看一下 函数sum(n=5)的递归执行过程,如下:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/ba/ba956794dbb7669b03947a5531895a39.jpeg","alt":"程序员必备的基本算法:递归详解","title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"计算sum(5)时,先sum(5)入栈,然后原问题sum(5)拆分为子问题sum(4),再入栈,直到终止条件sum(n=1)=1,就开始出栈。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"sum(1)出栈后,sum(2)开始出栈,接着sum(3)。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"最后呢,sum(1)就是后进先出,sum(5)是先进后出,因此递归过程可以理解为栈出入过程啦~"}]}]}]},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"递归的经典应用场景"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"哪些问题我们可以考虑使用递归来解决呢?即递归的应用场景一般有哪些呢?"}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"阶乘问题"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"二叉树深度"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"汉诺塔问题"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"斐波那契数列"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"快速排序、归并排序(分治算法也使用递归实现)"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"遍历文件,解析xml文件"}]}]}]},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"递归解题思路"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"解决递归问题一般就三步曲,分别是:"}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"第一步,定义函数功能"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"第二步,寻找递归终止条件"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"第二步,递推函数的等价关系式"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"这个递归解题三板斧理解起来有点抽象,我们拿阶乘递归例子来喵喵吧~"}]},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"1.定义函数功能"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"定义函数功能,就是说,你这个函数是干嘛的,做什么事情,换句话说,你要知道递归原问题是什么呀?比如你需要解决阶乘问题,定义的函数功能就是n的阶乘,如下:"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"//n的阶乘(n为大于0的自然数)\nint factorial (int n){\n\n}\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"2.寻找递归终止条件"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"递归的一个典型特征就是必须有一个终止的条件,即不能无限循环地调用本身。所以,用递归思路去解决问题的时候,就需要寻找递归终止条件是什么。比如阶乘问题,当n=1的时候,不用再往下递归了,可以跳出循环啦,n=1就可以作为递归的终止条件,如下:"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"//n的阶乘(n为大于0的自然数)\nint factorial (int n){\n    if(n==1){\n      return 1;\n    }\n}\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"3.递推函数的等价关系式"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"递归的"},{"type":"text","marks":[{"type":"strong"}],"text":"「本义」"},{"type":"text","text":",就是原问题可以拆为同类且更容易解决的子问题,即"},{"type":"text","marks":[{"type":"strong"}],"text":"「原问题和子问题都可以用同一个函数关系表示。递推函数的等价关系式,这个步骤就等价于寻找原问题与子问题的关系,如何用一个公式把这个函数表达清楚」"},{"type":"text","text":"。阶乘的公式就可以表示为 f(n) = n * f(n-1), 因此,阶乘的递归程序代码就可以写成这样,如下:"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"int factorial (int n){\n    if(n==1){\n      return 1;\n    }\n    return n * factorial(n-1);\n}\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"「注意啦」"},{"type":"text","text":",不是所有递推函数的等价关系都像阶乘这么简单,一下子就能推导出来。需要我们多接触,多积累,多思考,多练习递归题目滴~"}]},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"leetcode案例分析"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"来分析一道leetcode递归的经典题目吧~"}]},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"*"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"原题链接在这里哈:https://leetcode-cn.com/problems/invert-binary-tree/"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"*"}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"「题目:」"},{"type":"text","text":" 翻转一棵二叉树。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"输入:"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"     4\n   /   \\\n  2     7\n / \\   / \\\n1   3 6   9\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"输出:"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"     4\n   /   \\\n  7     2\n / \\   / \\\n9   6 3   1\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我们按照以上递归解题的三板斧来:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"「1. 定义函数功能」"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"函数功能(即这个递归原问题是),给出一颗树,然后翻转它,所以,函数可以定义为:"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"//翻转一颗二叉树\npublic TreeNode invertTree(TreeNode root) {\n}\n\n/**\n * Definition for a binary tree node.\n * public class TreeNode {\n *     int val;\n *     TreeNode left;\n *     TreeNode right;\n *     TreeNode(int x) { val = x; }\n * }\n */\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"「2.寻找递归终止条件」"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"这棵树什么时候不用翻转呢?当然是当前节点为null或者当前节点为叶子节点的时候啦。因此,加上终止条件就是:"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"//翻转一颗二叉树\npublic TreeNode invertTree(TreeNode root) {\n    if(root==null || (root.left ==null && root.right ==null)){\n       return root;\n    }\n}\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"「3. 递推函数的等价关系式」"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"原问题之你要翻转一颗树,是不是可以拆分为子问题,分别翻转它的左子树和右子树?子问题之翻转它的左子树,是不是又可以拆分为,翻转它左子树的左子树以及它左子树的右子树?然后一直翻转到叶子节点为止。嗯,看图理解一下咯~"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/62/629e73062cc04032b14c5cc10e50a54a.jpeg","alt":"程序员必备的基本算法:递归详解","title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"首先,你要翻转根节点为4的树,就需要"},{"type":"text","marks":[{"type":"strong"}],"text":"「翻转它的左子树(根节点为2)和右子树(根节点为7)」"},{"type":"text","text":"。这就是递归的"},{"type":"text","marks":[{"type":"strong"}],"text":"「递」"},{"type":"text","text":"的过程啦"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/24/2417fc5e61de859cfc41f2625779ce22.png","alt":"程序员必备的基本算法:递归详解","title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"然后呢,根节点为2的树,不是叶子节点,你需要继续"},{"type":"text","marks":[{"type":"strong"}],"text":"「翻转它的左子树(根节点为1)和右子树(根节点为3)」"},{"type":"text","text":"。因为节点1和3都是"},{"type":"text","marks":[{"type":"strong"}],"text":"「叶子节点」"},{"type":"text","text":"了,所以就返回啦。这也是递归的"},{"type":"text","marks":[{"type":"strong"}],"text":"「递」"},{"type":"text","text":"的过程~"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/2c/2cba5c2df58b2cc4c8f09b949f7a832c.jpeg","alt":"程序员必备的基本算法:递归详解","title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"同理,根节点为7的树,也不是叶子节点,你需要翻转"},{"type":"text","marks":[{"type":"strong"}],"text":"「它的左子树(根节点为6)和右子树(根节点为9)」"},{"type":"text","text":"。因为节点6和9都是叶子节点了,所以也返回啦。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/91/91948f237d924d47913a5df071222d0f.jpeg","alt":"程序员必备的基本算法:递归详解","title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"左子树(根节点为2)和右子树(根节点为7)都被翻转完后,这几个步骤就"},{"type":"text","marks":[{"type":"strong"}],"text":"「归来」"},{"type":"text","text":",即递归的归过程,翻转树的任务就完成了~"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/6e/6e632c462e9a952d4fcbdec20365afd4.png","alt":"程序员必备的基本算法:递归详解","title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"显然,"},{"type":"text","marks":[{"type":"strong"}],"text":"「递推关系式」"},{"type":"text","text":"就是:"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"invertTree(root)= invertTree(root.left) + invertTree(root.right);\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"于是,很容易可以得出以下代码:"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"//翻转一颗二叉树\npublic TreeNode invertTree(TreeNode root) {\n    if(root==null || (root.left ==null && root.right ==null){\n       return root;\n    }\n    //翻转左子树\n    TreeNode left = invertTree(root.left);\n    //翻转右子树\n    TreeNode right= invertTree(root.right);\n}\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"这里代码有个地方需要注意,翻转完一棵树的左右子树,还要交换它左右子树的引用位置。"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":" root.left = right;\n root.right = left;\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"因此,leetcode这个递归经典题目的"},{"type":"text","marks":[{"type":"strong"}],"text":"「终极解决代码」"},{"type":"text","text":"如下:"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"class Solution {\n    public TreeNode invertTree(TreeNode root) {\n         if(root==null || (root.left ==null && root.right ==null)){\n           return root;\n         }\n         //翻转左子树\n         TreeNode left = invertTree(root.left);\n         //翻转右子树\n         TreeNode right= invertTree(root.right);\n         //左右子树交换位置~\n         root.left = right;\n         root.right = left;\n         return root;\n    }\n}\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"拿终极解决代码去leetcode提交一下,通过啦~"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/db/dbb6d119362ce9e695d81a8b5ec7900e.png","alt":"程序员必备的基本算法:递归详解","title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"递归存在的问题"}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"递归调用层级太多,导致栈溢出问题"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"递归重复计算,导致效率低下"}]}]}]},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"栈溢出问题"}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"每一次函数调用在内存栈中分配空间,而每个进程的栈容量是有限的。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"当递归调用的层级太多时,就会超出栈的容量,从而导致调用栈溢出。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"其实,我们在前面小节也讨论了,递归过程类似于出栈入栈,如果递归次数过多,栈的深度就需要越深,最后栈容量真的不够咯"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"「代码例子如下:」"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"/**\n * 递归栈溢出测试\n */\npublic class RecursionTest {\n\n    public static void main(String[] args) {\n        sum(50000);\n    }\n    private static int sum(int n) {\n        if (n <= 1) {\n            return 1;\n        }\n        return sum(n - 1) + n;\n    }\n}\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"「运行结果:」"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"Exception in thread \"main\" java.lang.StackOverflowError\n at recursion.RecursionTest.sum(RecursionTest.java:13)\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"怎么解决这个栈溢出问题?首先需要"},{"type":"text","marks":[{"type":"strong"}],"text":"「优化一下你的递归」"},{"type":"text","text":",真的需要递归调用这么多次嘛?如果真的需要,先稍微"},{"type":"text","marks":[{"type":"strong"}],"text":"「调大JVM的栈空间内存」"},{"type":"text","text":",如果还是不行,那就需要弃用递归,"},{"type":"text","marks":[{"type":"strong"}],"text":"「优化为其他方案」"},{"type":"text","text":"咯~"}]},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"重复计算,导致程序效率低下"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我们再来看一道经典的青蛙跳阶问题:一只青蛙一次可以跳上1级台阶,也可以跳上2级台阶。求该青蛙跳上一个 n 级的台阶总共有多少种跳法。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"绝大多数读者朋友,很容易就想到以下递归代码去解决:"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"class Solution {\n    public int numWays(int n) {\n    if (n == 0){\n       return 1;\n     }\n    if(n <= 2){\n        return n;\n    }\n    return numWays(n-1) + numWays(n-2);\n    }\n}\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"但是呢,去leetcode提交一下,就有问题啦,超出时间限制了"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/ab/abda71c75fc1293c1f2759cfbfbb2ad2.png","alt":"程序员必备的基本算法:递归详解","title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"为什么超时了呢?递归耗时在哪里呢?先画出"},{"type":"text","marks":[{"type":"strong"}],"text":"「递归树」"},{"type":"text","text":"看看:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/56/566a40a9e116deb184077fe31232073e.png","alt":"程序员必备的基本算法:递归详解","title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"要计算原问题 f(10),就需要先计算出子问题 f(9) 和 f(8)"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"然后要计算 f(9),又要先算出子问题 f(8) 和 f(7),以此类推。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"一直到 f(2) 和 f(1),递归树才终止。"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我们先来看看这个递归的时间复杂度吧,"},{"type":"text","marks":[{"type":"strong"}],"text":"「递归时间复杂度 = 解决一个子问题时间*子问题个数」"}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"一个子问题时间 = f(n-1)+f(n-2),也就是一个加法的操作,所以复杂度是 "},{"type":"text","marks":[{"type":"strong"}],"text":"「O(1)」"},{"type":"text","text":";"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"问题个数 = 递归树节点的总数,递归树的总结点 = 2^n-1,所以是复杂度"},{"type":"text","marks":[{"type":"strong"}],"text":"「O(2^n)」"},{"type":"text","text":"。"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"因此,青蛙跳阶,递归解法的时间复杂度 = O(1) * O(2^n) = O(2^n),就是指数级别的,爆炸增长的,"},{"type":"text","marks":[{"type":"strong"}],"text":"「如果n比较大的话,超时很正常的了」"},{"type":"text","text":"。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"回过头来,你仔细观察这颗递归树,你会发现存在"},{"type":"text","marks":[{"type":"strong"}],"text":"「大量重复计算」"},{"type":"text","text":",比如f(8)被计算了两次,f(7)被重复计算了3次...所以这个递归算法低效的原因,就是存在大量的重复计算!"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"「那么,怎么解决这个问题呢?」"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"既然存在大量重复计算,那么我们可以先把计算好的答案存下来,即造一个备忘录,等到下次需要的话,先去"},{"type":"text","marks":[{"type":"strong"}],"text":"「备忘录」"},{"type":"text","text":"查一下,如果有,就直接取就好了,备忘录没有才再计算,那就可以省去重新重复计算的耗时啦!这就是"},{"type":"text","marks":[{"type":"strong"}],"text":"「带备忘录的解法」"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我们来看一下"},{"type":"text","marks":[{"type":"strong"}],"text":"「带备忘录的递归解法」"},{"type":"text","text":"吧~"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"一般使用一个数组或者一个哈希map充当这个"},{"type":"text","marks":[{"type":"strong"}],"text":"「备忘录」"},{"type":"text","text":"。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"假设f(10)求解加上"},{"type":"text","marks":[{"type":"strong"}],"text":"「备忘录」"},{"type":"text","text":",我们再来画一下递归树:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"「第一步」"},{"type":"text","text":",f(10)= f(9) + f(8),f(9) 和f(8)都需要计算出来,然后再加到备忘录中,如下:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/24/24e055469abd833d2bea295b906fd29a.png","alt":"程序员必备的基本算法:递归详解","title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"「第二步,」"},{"type":"text","text":" f(9) = f(8)+ f(7),f(8)= f(7)+ f(6), 因为 f(8) 已经在备忘录中啦,所以可以省掉,f(7),f(6)都需要计算出来,加到备忘录中~"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/78/78c73d347ca88ca81b7933721f9c5afa.png","alt":"程序员必备的基本算法:递归详解","title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"「第三步,」"},{"type":"text","text":" f(8) = f(7)+ f(6),发现f(8),f(7),f(6)全部都在备忘录上了,所以都可以剪掉。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/c1/c12311d7ce64469e853078f4fe2b2442.png","alt":"程序员必备的基本算法:递归详解","title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"所以呢,用了备忘录递归算法,递归树变成光秃秃的树干咯,如下:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/54/5453f27a34a96f960ea474d8a2a62227.png","alt":"程序员必备的基本算法:递归详解","title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"带「备忘录」的递归算法,子问题个数=树节点数=n,解决一个子问题还是O(1),所以"},{"type":"text","marks":[{"type":"strong"}],"text":"「带「备忘录」的递归算法的时间复杂度是O(n)」"},{"type":"text","text":"。接下来呢,我们用带「备忘录」的递归算法去撸代码,解决这个青蛙跳阶问题的超时问题咯~,代码如下:"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"public class Solution {\n    //使用哈希map,充当备忘录的作用\n    Map tempMap = new HashMap();\n    public int numWays(int n) {\n        // n = 0 也算1种\n        if (n == 0) {\n            return 1;\n        }\n        if (n <= 2) {\n            return n;\n        }\n        //先判断有没计算过,即看看备忘录有没有\n        if (tempMap.containsKey(n)) {\n            //备忘录有,即计算过,直接返回\n            return tempMap.get(n);\n        } else {\n            // 备忘录没有,即没有计算过,执行递归计算,并且把结果保存到备忘录map中,对1000000007取余(这个是leetcode题目规定的)\n            tempMap.put(n, (numWays(n - 1) + numWays(n - 2)) % 1000000007);\n            return tempMap.get(n);\n        }\n    }\n}\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/e4/e4296af607984df697f7f23d9fc0d876.png","alt":"程序员必备的基本算法:递归详解","title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"​"}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/e4/e4296af607984df697f7f23d9fc0d876.png","alt":"程序员必备的基本算法:递归详解","title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"写在最后"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"既然你们能看到这里说明这篇文章对你们的帮助还是有的,小编可不可以给你们索要一个小小的赞呢。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"延伸学习阅读"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://blog.csdn.net/javachengzi/article/details/109516189","title":null},"content":[{"type":"text","text":"还在为算法烦恼?那你应该还没看过这份Github上70k标星的笔记"}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"视频:"},{"type":"link","attrs":{"href":"https://www.bilibili.com/video/BV1WA411j7go/","title":null},"content":[{"type":"text","text":"暴力递归算法"}]}]},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"看完三件事️"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"========"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果你觉得这篇内容对你还蛮有帮助,我想邀请你帮我三个小忙:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"点赞,转发,有你们的 『点赞和评论』,才是我创造的动力。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"关注公众号 『 Java斗帝 』,不定期分享原创知识。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"同时可以期待后续文章ing"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}}]}

版权声明
本文为[InfoQ]所创,转载请带上原文链接,感谢
https://xie.infoq.cn/article/0d1cf877d9e0b31a16cd76486?utm_source=rss&utm_medium=article

Scroll to Top