码迷,mamicode.com
首页 > 其他好文 > 详细

[leetcode-572-Subtree of Another Tree]

时间:2017-05-07 12:59:22      阅读:147      评论:0      收藏:0      [点我收藏+]

标签:nod   ret   sts   logs   and   ant   because   tree   code   

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相等即可。

 

bool isSameTree(TreeNode* s, TreeNode* t)
{
    if(s == NULL &&  t== NULL) return true;
    if( (s == NULL && t != NULL )|| (s != NULL&&t == NULL) || (s->val !=t->val)) return false;
    bool left = isSameTree(s->left,t->left);
    bool right = isSameTree(s->right,t->right);
    return (left && right);
}
bool isSubtree(TreeNode* s, TreeNode* t)
{
    bool res = false;
    if(s!=NULL && t!= NULL)
    {
        if(t->val== s->val)      res = isSameTree(s,t);
        if(!res) res = isSubtree(s->left,t);
        if(!res) res = isSubtree(s->right,t);
    }
    return res;
}

 

[leetcode-572-Subtree of Another Tree]

标签:nod   ret   sts   logs   and   ant   because   tree   code   

原文地址:http://www.cnblogs.com/hellowooorld/p/6820227.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!