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.

 

Code:

复制代码
class Solution {
public:
    bool isSameTree(TreeNode *p, TreeNode *q) {
        if(p&&q){
            if(p->val!=q->val)
                return false;
            if(!isSameTree(p->left,q->left))
                return false;
            if(!isSameTree(p->right,q->right))
                return false;
            return true;
        }
        else if(!p&&!q)
            return true;
        else
            return false;
    }
};
复制代码

 

posted @   WinsCoder  阅读(170)  评论(0编辑  收藏  举报
(评论功能已被禁用)
点击右上角即可分享
微信分享提示