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 public class SubtreeofAnotherTree {
 2     static class TreeNode {
 3         int val;
 4         TreeNode left;
 5         TreeNode right;
 6         TreeNode(int x) {
 7             val = x;
 8         }
 9     }
10     //子树必须从叶结点开始,中间某个部分不是子树,转换一下思路,从某个节点s开始,
11     //跟t的所有结构都一样,那么问题就转换成判断两棵树是否相同
12     public boolean isSubtree(TreeNode s, TreeNode t) {
13 /*        if(s == null && t == null) {  //和27-34行效果相同
14             return true;
15         }
16         if(s == null) {
17             return false;
18         }
19         if(t == null) {
20             return true;
21         }
22         boolean result = false;
23         if(s.val == t.val) {
24             result = isSameTree(s, t);
25         }*/
26
27         if(t == null) {
28             return true;
29         }
30         if(s == null) {
31             return false;
32         }
33         boolean result = false;
34         result = isSameTree(s, t);  //判断是不是同一树
35
36
37         if(!result) {  //如果不是,则判断s的左子树是否和t相同
38             result = isSubtree(s.left, t);
39         }
40         if(!result) {  //如果仍不是,则判断s的右子树是否和t相同
41             result = isSubtree(s.right, t);
42         }
43         return result;
44     }
45
46     //判断两棵树是否是一样
47     public static boolean isSameTree(TreeNode root1, TreeNode root2) {
48         if(root1 == null && root2 == null) {  //最后都同时为null则该节点为叶节点,且相同
49             return true;
50         }
51         if(root1 == null || root2 == null) { //其中一个不为null,说明两节点的子节点不同,该节点也就不同
52             return false;
53         }
54         if(root1.val != root2.val) {  //值不同两节点也不同
55             return false;
56         }
57         return isSameTree(root1.left, root2.left) && isSameTree(root1.right, root2.right);
58     }
59 }

原文地址:https://www.cnblogs.com/xiyangchen/p/11100351.html

时间: 2024-07-30 08:18:10

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

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 /

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

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

判断一棵树是否是另一棵树的子树

问题 判断一棵树是否是另一棵树的子树,如图 思路 问题分两步: 找值相同的根结点(遍历解决) 判断两结点是否包含(递归:值.左孩子.右孩子分别相同) 代码 bool IsPart(TreeNode *root1, TreeNode *root2) { if (root2 == NULL) return true; if (root1 == NULL) return false; if (root1->val != root2->val) return false; return IsPart(