【leetcode】Same Tree

来源:互联网 发布:权威数据网站 编辑:程序博客网 时间:2024/04/30 02:19

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.
注意题目要求:左右子树不能互换,有些题目是可以互换的

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