给定彼此独立的两棵二叉树,树上的节点值两两不同,判断 t1 树是否有与 t2 树完全相同的子树。
子树指一棵树的某个节点的全部后继节点
数据范围:树的节点数满足
,树上每个节点的值一定在32位整型范围内
进阶:空间复杂度:
,时间复杂度
public boolean isContains (TreeNode root1, TreeNode root2) {
//判断r1是否包含r2?
if(root1==null) return false;
if(root1.val!=root2.val)//两个根节点不想等,则用root1的左右子树分别和root2比较
return isContains(root1.left,root2)||isContains(root1.right,root2);
else//root1和root2相同,继续对比
return isSame(root1,root2);
}
public boolean isSame(TreeNode root1,TreeNode root2){
if(root1==null&&root2==null) return true;
if(root1==null||root2==null) return false;
//两个根节点都不为空,继续比较
return (root1.val==root2.val)&&isSame(root1.left,root2.left)&&isSame(root1.right,root2.right);
} import java.util.*;
public class Solution {
public boolean isContains (TreeNode root1, TreeNode root2) {
// write code here
StringBuilder builder1 = new StringBuilder();
StringBuilder builder2 = new StringBuilder();
preOrder(root1,builder1);
preOrder(root2,builder2);
if(builder1.toString().contains(builder2.toString())){
return true;
}else {
return false;
}
}
//中序遍历并填充stringbuilder
static void preOrder(TreeNode treeNode,StringBuilder stringBuilder){
if(treeNode==null){
return;
}
preOrder(treeNode.left,stringBuilder);
stringBuilder.append(treeNode.val);
preOrder(treeNode.right,stringBuilder);
}
} #include <stdbool.h>
typedef struct TreeNode* NodePtr;
// function prototype
bool isSameTree(const NodePtr root1, const NodePtr root2);
/**
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
/**
*
* @param root1 TreeNode类
* @param root2 TreeNode类
* @return bool布尔型
*/
bool isContains(const NodePtr root1, const NodePtr root2) {
// 算法框架,逻辑上要找到通路!
if (!root1) return false;
return isSameTree(root1, root2)
|| isContains(root1->left, root2)
|| isContains(root1->right, root2);
}
bool isSameTree(const NodePtr root1, const NodePtr root2) {
if (!root1 || !root2) return root1 == root2;
return root1->val == root2->val
&& isSameTree(root1->left, root2->left)
&& isSameTree(root1->right, root2->right);
} //一种清晰些的写法,isEqual + isContains 看的人云里雾里
public boolean isContains (TreeNode root1, TreeNode root2) {
// write code here
if(root1 == null && root2 == null){
return true;
}
if(root1 == null || root2 == null){
return false;
}
if(root1.val == root2.val){
return isContains(root1.left, root2.left) && isContains(root1.right, root2.right);
}
return isContains(root1.left, root2) || isContains(root1.right, root2);
} /*
* function TreeNode(x) {
* this.val = x;
* this.left = null;
* this.right = null;
* }
*/
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param root1 TreeNode类
* @param root2 TreeNode类
* @return bool布尔型
*/
function isContains( root1 , root2 ) {
// write code here
function compare(r1, r2){
if(!r1 && !r2) return true
if(!r1 || !r2 || r1.val != r2.val) return false
if(r1.val == r2.val){
let left = compare(r1.left, r2.left)
let right = compare(r1.right, r2.right)
return left && right
}
}
function traverse(root){
if(!root) return null
if(root.val == root2.val){
// console.log('enter', root.val, root2.val)
res = compare(root, root2)
if(res) return true
}
traverse(root.left)
traverse(root.right)
}
let res = false
traverse(root1)
return res
}
module.exports = {
isContains : isContains
}; import java.util.*;
/*
* public class TreeNode {
* int val = 0;
* TreeNode left = null;
* TreeNode right = null;
* public TreeNode(int val) {
* this.val = val;
* }
* }
*/
public class Solution {
//遍历主树
//若数值相等,参试遍历同时遍历树进行判断
TreeNode sub=null;
public boolean check(TreeNode root1,TreeNode root2){
if(root1==null&&root2==null){
return true;
}
else if(root1==null||root2==null){
return false;
}
else{
if(root1.val!=root2.val){
return false;
}
else{
return check(root1.left,root2.left)&&check(root1.right,root2.right);
}
}
}
public boolean preOrder(TreeNode root){
if(root!=null){
if(root.val==sub.val){
if(check(root,sub)){
return true;
}
else return false;
}
return preOrder(root.left)||preOrder(root.right);
}
else return false;
}
public boolean isContains (TreeNode root1, TreeNode root2) {
if(root2==null)return true;
sub=root2;
//默认子树不空
return preOrder(root1);
}
}
public static boolean isContains(TreeNode root1, TreeNode root2) {
// 递归结束也没找到匹配的
if (root1 == null) return false;
if (root1.val != root2.val) {
return isContains(root1.left, root2) || isContains(root1.right, root2);
}
// 如果当前的值相同,则判断所有子树结构是否相同
return isEqual(root1, root2);
}
public static boolean isEqual(TreeNode root1, TreeNode root2) {
// 同时为空
if (root1 == null && root2 == null) return true;
// 某个提前为空
if (root1 == null || root2 == null) return false;
// 左边右边分别比较
return root1.val == root2.val && isEqual(root1.left, root2.left) && isEqual(root1.right, root2.right);
} public boolean isContains (TreeNode root1, TreeNode root2) {
// write code here
if(root1==null && root2==null){
return true;
}
if(root1==null||root2==null){
return false;
}
if(root1.val==root2.val){
return isSame(root1 ,root2);
}
return isContains(root1.left ,root2) || isContains(root1.right ,root2);
}
public boolean isSame(TreeNode root1 ,TreeNode root2){
if(root1==null && root2==null){
return true;
}
if(root1==null || root2==null){
return false;
}
if(root1.val!=root2.val){
return false;
}
return isSame(root1.left ,root2.left) && isSame(root1.right,root2.right);
} class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param root1 TreeNode类
* @param root2 TreeNode类
* @return bool布尔型
*/
bool isContains(TreeNode* root1, TreeNode* root2) {
// write code here
if (root1 == nullptr) return false;
if (dfs(root1, root2)) return true;
return isContains(root1->left, root2) || isContains(root1->right, root2);
}
bool dfs(TreeNode* root1, TreeNode* root2) {
if (root1 == nullptr && root2 == nullptr) return true;
else if (root1 != nullptr && root2 == nullptr) return false;
else if (root1 == nullptr && root2 != nullptr) return false;
if (root1->val != root2->val) return false;
return dfs(root1->left, root2->left) && dfs(root1->right, root2->right);
}
}; class Solution:
def isContains(self , root1: TreeNode, root2: TreeNode) -> bool:
# write code here
def isSameTree(root1, root2):
# 检查两个数是否相同
if root1 is None and root2 is None:
return True
if root1 is None or root2 is None or root1.val != root2.val:
return False
return isSameTree(root1.left, root2.left) and isSameTree(root1.right, root2.right)
# 基础情况:
# 1)两个都是空
if root1 is None and root2 is None:
return True
# 2)root1为空,root2非空,返回否
if root1 is None and root2 is not None:
return False
# 3)root1和root2是相同的树
if isSameTree(root1, root2):
return True
return self.isContains(root1.left, root2) or self.isContains(root1.right, root2)
package main
import . "nc_tools"
/*
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
/**
*
* @param root1 TreeNode类
* @param root2 TreeNode类
* @return bool布尔型
*/
func isContains( root1 *TreeNode , root2 *TreeNode ) bool {
if root1==nil&&root2==nil{
return true
}else if root1==nil||root2==nil{
return false
}else if root1.Val==root2.Val{
return check(root1,root2)
}else{
return isContains(root1.Left,root2)||isContains(root1.Right,root2)
}
}
func check(r1,r2 *TreeNode)bool{
if r1==nil&&r2==nil{
return true
}else if r1==nil||r2==nil||r1.Val!=r2.Val{
return false
}else{
return check(r1.Left,r2.Left)&&check(r1.Right,r2.Right)
}
} /**
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
/**
*
* @param root1 TreeNode类
* @param root2 TreeNode类
* @return bool布尔型
*/
//先找到root2的根在root1中的位置 提取root1中以root2的根为根的子树
//再判断两个子数是否完全相同
bool preorder(struct TreeNode* root1, struct TreeNode* root2) {
//static int count = 0;
if (NULL != root1 && NULL != root2) {
if (root1->val != root2->val)
return false;
return preorder(root1->left, root2->left) &&
preorder(root1->right, root2->right) ;
// preorder(root1->left, root2->left);
// preorder(root1->right, root2->right);
}
if (NULL != root1 && NULL == root2)
return false;
if (NULL == root1 && NULL != root2)
return false;
return true;
}
struct TreeNode* post(struct TreeNode* root,int value) {
//二叉树查找某个点
if(NULL==root)
return NULL;
struct TreeNode* temp=NULL;
if(root->left!=NULL){
temp=post(root->left,value);
if(temp!=NULL)
return temp;
}
if(root->right!=NULL){
temp=post(root->right,value);
if(temp!=NULL)
return temp;
}
if(root->val==value)
return root;
// post(root->left,value);
// post(root->right,value);
//arr[count++] = root->val;
return temp;
}
bool isContains(struct TreeNode* root1, struct TreeNode* root2 ) {
// write code here
struct TreeNode* temp=post(root1,root2->val);
if(temp==NULL){
return false;
}
return preorder(temp,root2);
} import java.util.*;
/*
* public class TreeNode {
* int val = 0;
* TreeNode left = null;
* TreeNode right = null;
* }
*/
public class Solution {
/**
*
* @param root1 TreeNode类
* @param root2 TreeNode类
* @return bool布尔型
*/
TreeNode root;
public boolean isContains(TreeNode root1, TreeNode root2) {
// write code here
if (root2 == null) {
return true;
}
middle(root1, root2);
if (root == null) {
return false;
} else {
return isEqual(root, root2);
}
}
private boolean isEqual(TreeNode root1, TreeNode root2) {
return middleValue(root1, root2);
}
private boolean middleValue(TreeNode root1, TreeNode root2) {
if (root1 == null && root2 == null) {
return true;
}
if ((root1 == null && root2 != null) || (root1 != null && root2 == null)) {
return false;
}
if (root1.val != root2.val) {
return false;
}
return middleValue(root1.left, root2.left) && middleValue(root1.right, root2.right);
}
private void middle(TreeNode root1, TreeNode root2) {
if (root1.val == root2.val) {
root = root1;
}
if (root1.left != null) {
middle(root1.left, root2);
}
if (root1.right != null) {
middle(root1.right, root2);
}
}
} class Solution: def postorder(self, root, res): if not root: return self.postorder(root.left, res) self.postorder(root.right, res) res.append(root.val) return def isContains(self , root1 , root2 ): res_1 = [] res_2 = [] self.postorder(root1, res_1) self.postorder(root2, res_2) print(res_1) print(res_2) for i in range(len(res_1)): if res_1[i] == res_2[0]: break for j in range(len(res_2)): if res_1[i] != res_2[j]: return False i += 1 return True
class Solution:
def isContains(self , root1: TreeNode, root2: TreeNode) -> bool:
def dfs_same(t1, t2):
if not t1 and not t2: return True
if not t1 or not t2: return False
l, r = dfs_same(t1.left, t2.left), dfs_same(t1.right, t2.right)
return l and r and t1.val == t2.val
def dfs(x):
if not x: return not root2
return dfs_same(x, root2) or dfs(x.left) or dfs(x.right)
return dfs(root1)
class Solution: def isContains(self , t1: TreeNode, t2: TreeNode) -> bool: # write code here if not t1 and t2: return False if not t2 and t1: return False if not t2 and not t1: return True if t1.val==t2.val: return self.isContains(t1.left, t2.left) and self.isContains(t1.right, t2.right) else: return self.isContains(t1.left, t2)&nbs***bsp;self.isContains(t1.right, t2)