LeetCode题解之 Subtree of Another Tree

Posted 山里的小勇子

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode题解之 Subtree of Another Tree相关的知识,希望对你有一定的参考价值。

1、题目描述

 

2、问题分析

判断一个节点,然后判断子树。

 

3、代码

 1  bool isSubtree(TreeNode* s, TreeNode* t) {
 2        if (s == NULL)
 3            return false;
 4        else {
 5            return isSame(s,t) || isSubtree(s->left, t) || isSubtree(s->right, t);
 6        }
 7         
 8     }
 9     
10     bool isSame(TreeNode *t1, TreeNode *t2)
11     {
12         if (t1 == NULL && t2 == NULL) return true;
13         if (t1 == NULL || t2 == NULL) return false;
14         
15         return (t1->val == t2->val) && isSame(t1->left, t2->left) && isSame(t1->right , t2->right); 
16     }
17     

 

以上是关于LeetCode题解之 Subtree of Another Tree的主要内容,如果未能解决你的问题,请参考以下文章

[leetcode-572-Subtree of Another Tree]

leetcode 572. Subtree of Another Tree

LeetCode 572: Subtree of Another Tree

Leetcode 572. Subtree of Another Tree

leetcode572. Subtree of Another Tree

[LeetCode] 572. Subtree of Another Tree