LeetCode 572. 另一个树的子树

我的LeetCode:https://leetcode-cn.com/u/ituring/

我的LeetCode刷题源码[GitHub]:https://github.com/izhoujie/Algorithmcii

LeetCode 572. 另一个树的子树

题目

给定两个非空二叉树 s 和 t,检验 s 中是否包含和 t 具有相同结构和节点值的子树。s 的一个子树包括 s 的一个节点和这个节点的所有子孙。s 也可以看做它自身的一棵子树。

示例 1:

给定的树 s:

     3
    / 
   4   5
  / 
 1   2
给定的树 t:

   4 
  / 
 1   2
返回 true,因为 t 与 s 的一个子树拥有相同的结构和节点值。

示例 2:

给定的树 s:

     3
    / 
   4   5
  / 
 1   2
    /
   0
给定的树 t:

   4
  / 
 1   2
返回 false。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/subtree-of-another-tree
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

解题思路

与本题相似的题目,可对对比学习:

本题当前只写了DFS递归判断题解;
另外还有:

  • KMP解法;
  • hash解法;
  • 埃氏筛选法;

以后待研究..

思路1-递归判断,t是否为s本身或者是其子树之一

t若是s的一个子树,那么t等于s或者是s的左右子树之一;
步骤:

  1. 判断t是否是s本身,若不是则递归判断t是否是s的左右子树之一;

算法复杂度: M为s的总节点数,N为t的节点数

  • 时间复杂度: $ {color{Magenta}{Omicronleft(M * N ight)}} $
  • 空间复杂度: $ {color{Magenta}{Omicronleft(max(M, N) ight)}} $

算法源码示例

package leetcode;

/**
 * @author ZhouJie
 * @date 2020年5月7日 上午12:45:22 
 * @Description: 572. 另一个树的子树
 *
 */
public class LeetCode_0572 {

}

//  Definition for a binary tree node.
class TreeNode_0572 {
	int val;
	TreeNode_0572 left;
	TreeNode_0572 right;

	TreeNode_0572(int x) {
		val = x;
	}
}

class Solution_0572 {
	/**
	 * @author: ZhouJie
	 * @date: 2020年5月7日 上午12:46:19 
	 * @param: @param s
	 * @param: @param t
	 * @param: @return
	 * @return: boolean
	 * @Description: 1-递归判断t是不是s本身或者是其子树之一;
	 *
	 */
	public boolean isSubtree_1(TreeNode_0572 s, TreeNode_0572 t) {
		if (s == null && t == null) {
			return true;
		} else if (s != null && t != null) {
			// t可能是s本身或者是s的左子树/右子树
			return checkSubtree(s, t) || isSubtree_1(s.left, t) || isSubtree_1(s.right, t);
		} else {
			return false;
		}
	}

	/**
	 * @author: ZhouJie
	 * @date: 2020年5月7日 上午12:52:54 
	 * @param: @param s
	 * @param: @param t
	 * @param: @return
	 * @return: boolean
	 * @Description: 是否子树判断
	 *
	 */
	private boolean checkSubtree(TreeNode_0572 s, TreeNode_0572 t) {
		if (s == null && t == null) {
			return true;
		} else if (s != null && t != null && s.val == t.val) {
			return checkSubtree(s.left, t.left) && checkSubtree(s.right, t.right);
		} else {
			return false;
		}
	}
}

原文地址:https://www.cnblogs.com/izhoujie/p/12852914.html