LeetCode题解之 Subtree of Another Tree

1、题目描述

LeetCode题解之 Subtree of Another Tree

2、问题分析

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

3、代码

  bool isSubtree(TreeNode* s, TreeNode* t) {
if (s == NULL)
return false;
else {
return isSame(s,t) || isSubtree(s->left, t) || isSubtree(s->right, t);
} } bool isSame(TreeNode *t1, TreeNode *t2)
{
if (t1 == NULL && t2 == NULL) return true;
if (t1 == NULL || t2 == NULL) return false; return (t1->val == t2->val) && isSame(t1->left, t2->left) && isSame(t1->right , t2->right);
}
上一篇:LeetCode题解之 Increasing Order Search Tree


下一篇:mac攻略(二) -- 简单配置php开发环境