您的位置:首页 > 其它

Same Tree LeetCode

2014-08-13 11:05 239 查看
https://oj.leetcode.com/problems/same-tree/

public boolean isSameTree(TreeNode p, TreeNode q) {
if(p==null&&q==null){
return true;
}
if(p==null){
return false;
}
if(q==null){
return false;
}
Stack<TreeNode> stackA= new Stack<TreeNode>();
Stack<TreeNode> stackB= new Stack<TreeNode>();

stackA.push(p);
stackB.push(q);

while(!stackA.empty()&&!stackB.empty()){
TreeNode a= stackA.pop();
TreeNode b= stackB.pop();

if(a.val!=b.val){
return false;
}

if(a.left!=null){
if(b.left==null){
return false;
}
stackA.push(a.left);
stackB.push(b.left);
}else{
if(b.left!=null){
return false;
}
}
if(a.right!=null){
if(b.right==null){
return false;
}
stackA.push(a.right);
stackB.push(b.right);
}else{
if(b.right!=null){
return false;
}
}

}
return true;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: