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

100 Same Tree

时间:2015-08-19 07:08:34      阅读:146      评论:0      收藏:0      [点我收藏+]

标签:

100 Same Tree

链接:https://leetcode.com/problems/same-tree/
问题描述:
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 have the same value.

Hide Tags Tree Depth-first Search

判断两棵树是否完全相同,只要每个节点拿出来比对就可以。

class Solution {
public:
    bool isSameTree(TreeNode* p, TreeNode* q) {
        return misSameTree(p,q);
    }
    bool misSameTree(TreeNode* n1,TreeNode* n2)
    {
        if(n1==NULL&&n2==NULL)return true;
        else if(n1==NULL||n2==NULL) return false;
        else if(n1->val!=n2->val)return false;
        else return misSameTree(n1->left,n2->left)&&misSameTree(n1->right,n2->right);
    }
};

版权声明:本文为博主原创文章,未经博主允许不得转载。

100 Same Tree

标签:

原文地址:http://blog.csdn.net/efergrehbtrj/article/details/47772441

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