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.

Solution: recursion.

1 class Solution {
2 public:
3     bool isSameTree(TreeNode *p, TreeNode *q) {
4         if(!p && !q) return true;
5         if((!p && q) || (p && !q)) return false;
6         if(p->val != q->val) return false;
7         return isSameTree(p->left, q->left) && isSameTree(p->right, q->right);
8     }
9 };
原文地址:https://www.cnblogs.com/zhengjiankang/p/3646295.html