LeetCode 572. 另一个树的子树

题目链接:https://leetcode-cn.com/problems/subtree-of-another-tree/

给定两个非空二叉树 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。

思路:

一个树是另一个树的子树 则

  • 要么这两个树相等
  • 要么这个树是左树的子树
  • 要么这个树hi右树的子树
 1 /**
 2  * Definition for a binary tree node.
 3  * struct TreeNode {
 4  *     int val;
 5  *     struct TreeNode *left;
 6  *     struct TreeNode *right;
 7  * };
 8  */
 9 bool isSame(struct TreeNode* s, struct TreeNode* t)
10 {
11     if(s!=NULL&&t==NULL) return false;
12     if(t!=NULL&&s==NULL) return false;
13     if(s==NULL&&t==NULL) return true;
14     if(s->val!=t->val) return false;
15     return isSame(s->left,t->left)&&isSame(s->right,t->right);
16 }
17 bool isSubtree(struct TreeNode* s, struct TreeNode* t){
18     if(s==NULL) return false;
19     if(isSame(s,t)) return true;
20     return isSubtree(s->left,t)||isSubtree(s->right,t);
21 }

原文地址:https://www.cnblogs.com/shixinzei/p/11367414.html

时间: 2024-08-30 04:56:49

LeetCode 572. 另一个树的子树的相关文章

LeetCode 572. 另一个树的子树(Subtree of Another Tree) 40

572. 另一个树的子树 572. Subtree of Another Tree 题目描述 给定两个非空二叉树 s 和 t,检验?s 中是否包含和 t 具有相同结构和节点值的子树.s 的一个子树包括 s 的一个节点和这个节点的所有子孙.s 也可以看做它自身的一棵子树. 每日一算法2019/6/12Day 40LeetCode572. Subtree of Another Tree 示例 1: 给定的树 s: 3 / 4 5 / 1 2 给定的树 t: 4 / 1 2 返回 true,因为 t

572. 另一个树的子树

给定两个非空二叉树 s 和 t,检验 s 中是否包含和 t 具有相同结构和节点值的子树.s 的一个子树包括 s 的一个节点和这个节点的所有子孙.s 也可以看做它自身的一棵子树. 示例 1:给定的树 s: 3 / \ 4 5 / \ 1 2给定的树 t: 4 / \ 1 2返回 true,因为 t 与 s 的一个子树拥有相同的结构和节点值 来源:力扣(LeetCode)链接:https://leetcode-cn.com/problems/subtree-of-another-tree 1 pub

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 considere

编程判断一个树是完全二叉树(使用层次遍历实现)

完全二叉树:一棵具有N个节点的二叉树的结构与满二叉树的前N个节点的结构相同 如何判断一个树是完全二叉树 可以使用层序遍历,只需2个步骤 第一步:如果遍历到一个节点只有右子树没有左子树,则不是完全二叉树 第二部:如果遍历到一个节点只有左子树,那么后面遍历到的节点必须是叶子节点,否则也不是完全二叉树 排除以上两种情况,则树是完全二叉树 核心代码: //层序遍历 int LayerOrder(BiTreeNode *head) { bool flag=0; LQueue Q; Initiate_Que

15.Subtree of Another Tree(判断一棵树是否为另一颗树的子树)

Level: ??Easy 题目描述: 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 cou

树的子树

判断一棵树是否是另一棵树的子树 public class Solution { public boolean HasSubtree(TreeNode root1,TreeNode root2) { boolean result = false; if(root1 == null || root2 == null) return false; if(root1!=null && root2!=null){ if(root1.val == root2.val){ result = SameTr

[LeetCode] Scramble String(树的问题最易用递归)

Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrings recursively. Below is one possible representation of s1 = "great": great / gr eat / \ / g r e at / a t To scramble the string, we may choose a

boost库使用:仿SGI-STL实现的一个树节点内存allocator

1 ////////////////////////////////////////////////////////////////////////// 2 //code by hzs 3 //email: [email protected] 4 //Last modified: 2014-5-18 21:05 5 ////////////////////////////////////////////////////////////////////////// 6 7 #ifndef _TRE

28、输入两棵二叉树A,B,判断B是不是A的子结构。(ps:我们约定空树不是任意一个树的子结构)

题目描述 输入两棵二叉树A,B,判断B是不是A的子结构.(ps:我们约定空树不是任意一个树的子结构) 思路:  1.当Tree1和Tree2都不为零的时候,才进行比较.否则直接返回false  2. 2.1如果找到了对应Tree2的根节点的点, 以这个根节点为为起点判断是否包含Tree2 2.2 如果找不到,那么就再去root的左孩子当作起点,去判断时候包含Tree22.3 如果还找不到,那么就再去root的右孩子当作起点,去判断时候包含Tree2 1 public class Solution