链接
题意
判断两棵树是否一样
思路
同时遍历两棵树,判断相对应的结点是否相等即可
代码
C:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
bool isSameTree(struct TreeNode* p, struct TreeNode* q) {
return (p==NULL || q==NULL)? (p==q) :p->val==q->val && isSameTree(p->left, q->left) && isSameTree(p->right, q->right);
}