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 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

思路分析:

??判断t树是否为s树的子树,先在s中找到和t根节点相同的节点,然后从该节点出发判断是否存在与t完全相同的结构。

代码:

public class TreeNode{
    int val;
    TreeNode left;
    TreeNode right;
    public TreeNode(int x){
        val=x;
    }
}
public class Solution{
    public boolean isSubtree(TreeNode s,TreeNode t){
        if(s==null||t==null)
            return false;
        return checkTree(s,t)||isSubtree(s.left,t)||isSubtree(s.right,t);//找到与t根节点相同的节点,然后开始进行判断
    }
    public boolean checkTree(TreeNode s,TreeNode t){
        if(s==null&&t==null)  //同时为null证明结构一样
            return true;
        if(s==null||t==null)  //如果任意一个不为空,代表结构不一样
            return false;
        return (s.val==t.val)&&checkTree(s.left,t.left)&&checkTree(s.right,t.right);//这是判断结构是否相同的条件,对应节点值相同,并且左右子树对应的结构也要相同。
    }
}

原文地址:https://www.cnblogs.com/yjxyy/p/10712422.html

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

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

【LeetCode】Symmetric Tree 判断一棵树是否是镜像的

题目:Symmetric Tree <span style="font-size:18px;">/**LeetCode Symmetric Tree 对称的树 * 思路:判断一棵树是否对称,1.有左子树就要有右子树 * 2.除根节点外对称节点值要相同 * 注意:对称后就是左子树的左节点和右子树的右节点比较 * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; *

LeetCode 100. Same Tree 判断两棵二叉树是否相等

Given two binary trees, write a function to check if they are the same or not. Two binary trees are considered the same if they are structurally identical and the nodes have the same value. Example 1: Input: 1 1 / \ / 2 3 2 3 [1,2,3], [1,2,3] Output:

【easy】572. Subtree of Another Tree

判断一棵树中是否包含另一棵子树(包含是,两棵树重合处的根节点之下的子节点都相等) 有两种方法: 方法二:递归写法 //方法一:可以借鉴之前序列化的题目,如果序列化得到的序列一样就是相同的树 //方法二:用递归来写十分的简洁,我们先从s的根结点开始,跟t比较,如果两棵树完全相同,那么返回true,否则就分别对s的左子结点和右子结点调用递归再次来判断是否相同,只要有一个返回true了,就表示可以找得到. class Solution { public: bool isSubtree(TreeNode

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

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

比较两棵二叉树--(比较两棵二叉树是否相同/判断一棵二叉树是否是另一棵二叉树的子树)

一,问题介绍 本文章讨论两个问题: ①如何判断两棵二叉树的结构是一样的.对应的每个结点都有着相同的值.--即判断两棵二叉树是一样的 ②给定两棵二叉树,如何判断一棵二叉树是另一棵二叉树的子结构 ③给定两棵二叉树,如何判断一棵二叉树是另一棵二叉树的子树 注意,子结点与子树有那么一点点不同. 上面的二叉树B 是二叉树A 的子结构,但是不能说是二叉树A的子树.但是二叉树C 是 二叉树A的子树. 二,问题分析 1,如何判断两棵二叉树的结构是一样的.且对应的每个结点都有着相同的值. 对于①如何判断两棵二叉树

LeetCode:Same Tree - 判断两颗树是否相等

1.题目名称 Same Tree(判断两棵树是否相等) 2.题目地址 https://leetcode.com/problems/same-tree/ 3.题目内容 英文:Given two binary trees, write a function to check if they are equal or not. Two binary trees are considered equal if they are structurally identical and the nodes h

判断一棵二叉树是不是平衡二叉树

二叉树中任意左右子树的深度相差不超过1,那么它就是一棵平衡二叉树. 两种解法. 第一种:菜鸟的解法,出现重复遍历,时间复杂度高. 1 bool IsBalanced(BinaryTreeNode* root) 2 { 3 if (root == NULL) 4 { 5 return true ; 6 } 7 int left = TreeDepth(root->m_pLeft);//该函数实现见我上一篇博客"数的深度" 8 int right = TreeDepth(root-&

编程实现判断一棵二叉树是否是平衡二叉树

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. 思路:遍历这棵二叉树,每访问