美文网首页
100. Same Tree

100. Same Tree

作者: YellowLayne | 来源:发表于2017-06-17 15:36 被阅读0次

1.描述

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.

2.分析

3.代码

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */
bool isSameTree(struct TreeNode* p, struct TreeNode* q) {
    if (NULL == p && NULL == q) return true;
    if (NULL == p || NULL == q) return false;
    if (p->val != q->val) return false;
    
    bool left  = isSameTree(p->left, q->left);
    bool right = isSameTree(p->right, q->right);
    
    return left && right ? true : false;
}

相关文章

网友评论

      本文标题:100. Same Tree

      本文链接:https://www.haomeiwen.com/subject/hqfbqxtx.html