标签:problem mamicode tree node struct nod lan ble tree tps
题目链接:https://leetcode-cn.com/problems/same-tree/
给定两个二叉树,编写一个函数来检验它们是否相同。
如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的。
输出: true
输出: false
输出: false
1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * struct TreeNode *left; 6 * struct TreeNode *right; 7 * }; 8 */ 9 10 bool isSameTree(struct TreeNode* p, struct TreeNode* q){ 11 if(p==NULL&&q==NULL) return true; 12 else if(p&&q){ 13 if(p->val==q->val){ 14 return isSameTree(p->left,q->left)&&isSameTree(p->right,q->right); 15 } 16 else return false; 17 } 18 else return false; 19 }
标签:problem mamicode tree node struct nod lan ble tree tps
原文地址:https://www.cnblogs.com/shixinzei/p/11605279.html