首页 > 其他 > 详细

100 Same Tree

时间:2015-08-19 07:06:13      阅读:97      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!