写在前面
终于鼓起勇气来写二叉树的递归问题了,不得不说递归对我来说一直是个弱项,总感觉递归的方式不好理解,这篇文章的主要目的就是梳理清楚递归问题的通用解决方式,为以后做题提出一个通用的指导思想。
递归问题的解
递归问题的求解其实可以分为两种,一种我们称为自顶向下,另一种就是反过来的自底向上了,自顶向下的递归,通常要求我们的每一步都计算出结果,最后再进行汇总操作,而自底向上的递归,则通常是从最基本问题开始计算,然后回溯至问题顶部,这样说比较抽象,可以看下面两个具体的例子。
leetcode 572. Subtree of Another Tree
题目描述
Given two non-empty binary trees s and t, check whether tree t has exactly the same structure and node values with a subtree of s. A subtree of s is a tree consists of a node in s and all of this node’s descendants. The tree s could also be considered as a subtree of itself.
Example 1:
Given tree s:
3
/ \
4 5
/ \
1 2
Given tree t:
4
/ \
1 2
Return true, because t has the same structure and node values with a subtree of s.
Example 2:
Given tree s:
3
/ \
4 5
/ \
1 2
/
0
Given tree t:
4
/ \
1 2
Return false.
题目分析
我相信大多数人看到这个题的第一反应就是好简单啊。它确实很简单,但也确实跟大多数二叉树递归的题目一样,给我造成了很大的困扰。所以在上手去解这样的问题之前,我们一定要分析清楚问题,首先,我们很容易剥离出一个子问题出来,那就是s的子树是否与t相同结构,那么可以把它写成一个函数形式,等待后面调用。另外一个子问题,我们知道,要判断s中是否包含t的结构,我们必须遍历s的所有子树结构,那么这个子问题就包含了上述第一个子问题,他们之间是包含关系。
我们再来分析递归调用的过程。我们先看第一个子问题,很明显,我们需要在每一次的递归中去求解和判断,这显然是一个自顶向下的问题,我们再看第二个子问题,同样的,跟第一个问题的结构类似,我们也需要做类似的判断过程,因此这个问题就分析地很清晰了,我们只需要在外部递归关系中调用第一个子问题作为求解过程,整个问题就解决了。
在分析的过程中,我们发现这个问题确实是不难的,关键就在于在遇到递归问题时不要急着去写代码,先分析好问题类型,是自顶向下还是自底向上?然后做好问题分解,需不需要嵌套递归?这样才会起到事半功倍的效果。
AC题解
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool isSubtree(TreeNode* s, TreeNode* t) {
if(s==nullptr)
return false;
if(isSame(s,t)==false) return isSubtree(s->left,t)||isSubtree(s->right,t);
return true;
}
bool isSame(TreeNode *s,TreeNode*t) {
if(s==nullptr||t==nullptr)
return s==t;
if(s->val==t->val) return isSame(s->left,t->left)&&isSame(s->right,t->right);
return false;
}
};
自底向上的递归问题求解
leetcode 110这道题可以用自顶向下和自底向上两种方式求解,我们将对两种解法一并介绍。
leetcode 110. Balanced Binary Tree
题目描述
Given a binary tree, determine if it is height-balanced.
For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1.
题目分析
这道题让我们写一个函数判断一颗二叉树是不是高度平衡的二叉树,关于高度平衡的定义,下面也说的很清楚了,每颗节点的左右子树的高度差不超过1。我们先考虑简单的解法,首先,尝试分解一下问题,如果每颗子树的高度差不超过1,我们可以根据结点去算每颗结点对应子树的高度,然后递归遍历就可以了,这显然是自顶向下的算法,我们先写出这种方式计算的AC解。
自顶向下的AC题解
首先:
子函数求树的高度
int depth(TreeNode* root) {
if(root==nullptr) return 0;
int left = depth(root->left)+1;
int right = depth(root->right)+1;
return max(left,right);
}
其次:
主函数递归调用子函数:
bool isBalanced(TreeNode* root) {
if(root==nullptr)return true;
int left = depth(root->left);
int right = depth(root->right);
return abs(left-right)<=1&&isBalanced(root->left)&&isBalanced(root->right);
}
很容易确认上述过程的正确性,但问题也是显而易见的,我们频繁地计算了子树高度,对每一个节点的访问都多于1次,这显然不合理,如果是自底向上的考虑,每一种子树的情况从底部都已经被考虑过了,所有的节点都只被访问一次,问题的关键就在于如何设计自底向上过程。
自底向上的AC题解
一个自然的考虑,如果是自底向上的过程,问题自然就简化成了一个问题的递归:子树是否是height-balanced?所以在本题中,不存在需要重复调用的子问题。我们需要对求高度的过程进行改造,在高度求解的过程中,找到问题的解。
递归的最终问题还是子树高度的差值,一旦出现子树高度差值不满足条件的情况,退出递归并返回false即可。
因为我们本质上还是利用高度来做判断,我们用-1作为递归退出条件。(某棵子树不满足条件全递归栈都返回)
修改后的代码如下:
int depth(TreeNode* root) {
if(root==nullptr) return 0;
int left = 0,right = 0;
left = depth(root->left);
if(left == -1) return -1;
right = depth(root->right);
if(right==-1)return -1;
if(abs(left-right)>1)return -1;
return max(left,right)+1;
}
主函数
bool isBalanced(TreeNode* root) {
return depth(root)!=-1;
}
后记
自底向上的解法的本质其实还是利用递归栈的性质,不断地出栈过程提供了可利用上次结果的可能性。