【二叉树】判断两棵树是否相同

xiaoxiao2021-02-28  70

题目链接:https://leetcode.com/problems/same-tree/#/description

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

转载请注明原文地址: https://www.6miu.com/read-79439.html

最新回复(0)