100 Same Tree
链接:https://leetcode.com/problems/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.
Hide Tags Tree Depth-first Search
判断两棵树是否完全相同,只要每个节点拿出来比对就可以。
class Solution {
public:
bool isSameTree(TreeNode* p, TreeNode* q) {
return misSameTree(p,q);
}
bool misSameTree(TreeNode* n1,TreeNode* n2)
{
if(n1==NULL&&n2==NULL)return true;
else if(n1==NULL||n2==NULL) return false;
else if(n1->val!=n2->val)return false;
else return misSameTree(n1->left,n2->left)&&misSameTree(n1->right,n2->right);
}
};