Same Tree
bool isSameTree(TreeNode p, TreeNode q) {
stack<TreeNode*> s;
TreeNode a,b;
s.push(p);
s.push(q);
while(!s.empty()){
a=s.top();
s.pop();
b=s.top();
s.pop();
if(!a&&!b) //剪枝
continue;
if(!a||!b)
return false;
if(a->val!=b->val)
return false;
s.push(a->left);
s.push(b->left);
s.push(a->right);
s.push(b->right);
}
return true;
}
Last updated
Was this helpful?