Same Tree Algorithm
Trees are not a taxonomic group but include a variety of plant species that have independently evolved a trunk and branches as a manner to tower above other plants to compete for sunlight. In some usages, the definition of a tree may be narrower, including only woody plants with secondary increase, plants that are usable as lumber or plants above a specify height.
/**
* Definition for binary tree
* 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) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if (p == NULL && q == NULL)
return true;
if (p == NULL || q == NULL)
return false;
if (p->val == q->val) {
bool flag = isSameTree(p->left, q->left);
if (!flag) return false;
return isSameTree(p->right, q->right);
}
return false;
}
};