Yu's Coding Garden : leetcode Question 90: Same Tree
Given two binary trees, write a function to check if they are equal or not.
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
Recursively check the left child and right child. If the value is different, or if one of the two nodes is null, return false.
Recursively check the left child and right child. If the value is different, or if one of the two nodes is null, return false.
bool
isSameTree(TreeNode *p, TreeNode *q) {
if
(!p && !q) {
return
true
;}
if
((!p && q) || (!q && p)){
return
false
;}
if
(p->val!=q->val){
return
false
;}
return
isSameTree(p->left,q->left) && isSameTree(p->right,q->right);
}
http://www.cnblogs.com/lautsie/p/3247097.html
public
boolean
isSameTree(TreeNode p, TreeNode q) {
// Start typing your Java solution below
// DO NOT write main() function
if
(p ==
null
&& q ==
null
)
return
true
;
if
(p !=
null
&& q !=
null
) {
if
(p.val == q.val &&
isSameTree(p.left, q.left) &&
isSameTree(p.right, q.right))
return
true
;
}
return
false
;
}
public
boolean
isSameTree(TreeNode p, TreeNode q) {
return
isSame(p, q);
}
private
boolean
isSame(TreeNode p, TreeNode q) {
if
(p ==
null
)
return
q ==
null
;
if
(q ==
null
)
return
false
;
if
(p.val != q.val)
return
false
;
if
(isSame(p.left, q.left) ==
false
)
return
false
;
if
(isSame(p.right, q.right) ==
false
)
return
false
;
return
true
;
}
X.
The iterative solution using queue is a bit of tricky because we allow enqueue null into the queue, and utilized the null elements to check if the queue is the same.
public
boolean
isSameTree(TreeNode p, TreeNode q) {
if
(p ==
null
&& q ==
null
)
return
true
;
Queue<TreeNode> lQueue =
new
LinkedList<TreeNode>();
Queue<TreeNode> rQueue =
new
LinkedList<TreeNode>();
lQueue.offer(p);
rQueue.offer(q);
if
(lQueue.isEmpty() || rQueue.isEmpty())
return
false
;
while
(!lQueue.isEmpty() && !rQueue.isEmpty()) {
TreeNode lCurr = lQueue.poll();
TreeNode rCurr = rQueue.poll();
if
(lCurr ==
null
&& rCurr ==
null
)
continue
;
if
(lCurr ==
null
|| rCurr ==
null
)
return
false
;
if
(lCurr.val != rCurr.val)
return
false
;
lQueue.offer(lCurr.left);
lQueue.offer(lCurr.right);
rQueue.offer(rCurr.left);
rQueue.offer(rCurr.right);
}
return
true
;
}
public boolean isSameTree(TreeNode p, TreeNode q) { // iteration method if (p == null && q == null) return true; if (p == null && q != null || p != null && q == null) return false; Stack<TreeNode> stackP = new Stack<>(); Stack<TreeNode> stackQ = new Stack<>(); stackP.add(p); stackQ.add(q); while (!stackP.isEmpty() && !stackQ.isEmpty()) { TreeNode tmpP = stackP.pop(); TreeNode tmpQ = stackQ.pop(); if (tmpP.val != tmpQ.val) return false; if (tmpP.left != null && tmpQ.left != null) { stackP.push(tmpP.left); stackQ.push(tmpQ.left); } else if (tmpP.left == null && tmpQ.left == null) { } else { return false; } if (tmpP.right != null && tmpQ.right != null) { stackP.push(tmpP.right); stackQ.push(tmpQ.right); } else if (tmpP.right == null && tmpQ.right == null) { } else { return false; } } if (!stackP.isEmpty() || !stackQ.isEmpty()) return false; return true; }
https://leetcode.com/discuss/22197/my-non-recursive-method
public boolean isSameTree(TreeNode p, TreeNode q) { Stack<TreeNode> stack_p = new Stack <> (); Stack<TreeNode> stack_q = new Stack <> (); if (p != null) stack_p.push( p ) ; if (q != null) stack_q.push( q ) ; while (!stack_p.isEmpty() && !stack_q.isEmpty()) { TreeNode pn = stack_p.pop() ; TreeNode qn = stack_q.pop() ; if (pn.val != qn.val) return false ; if (pn.right != null) stack_p.push(pn.right) ; if (qn.right != null) stack_q.push(qn.right) ; if (stack_p.size() != stack_q.size()) return false ; if (pn.left != null) stack_p.push(pn.left) ; if (qn.left != null) stack_q.push(qn.left) ; if (stack_p.size() != stack_q.size()) return false ;//\\ } return stack_p.size() == stack_q.size() ; }
http://n00tc0d3r.blogspot.com/2013/05/same-tree.html
Solution - Iteration
Also http://www.geeksforgeeks.org/write-c-code-to-determine-if-two-trees-are-identical/
http://www.geeksforgeeks.org/iterative-function-check-two-trees-identical/
To identify if two trees are identical, we need to traverse both trees simultaneously, and while traversing we need to compare data and children of the trees.
The idea is to use level order traversal. We traverse both trees simultaneously and compare the data whenever we dequeue and item from queue.
http://www.cnblogs.com/lautsie/p/3247097.html
Read full article from Yu's Coding Garden : leetcode Question 90: Same Tree
public boolean isSameTree(TreeNode p, TreeNode q) {
if (p == null && q == null) return true;
if (p == null || q == null || p.val != q.val) return false;
Queue<TreeNode> pque = new ArrayDeque<TreeNode>();
Queue<TreeNode> qque = new ArrayDeque<TreeNode>();
pque.add(p); qque.add(q);
while (!pque.isEmpty() && !qque.isEmpty()) {
TreeNode pp = pque.remove();
TreeNode qq = qque.remove();
if (pp.left != null && qq.left != null && pp.left.val == qq.left.val) {
pque.add(pp.left); qque.add(qq.left);
} else if (!(pp.left == null && qq.left == null)) {
return false;
}
if (pp.right != null && qq.right != null && pp.right.val == qq.right.val) {
pque.add(pp.right); qque.add(qq.right);
} else if (!(pp.right == null && qq.right == null)) {
return false;
}
}
return (pque.isEmpty() && qque.isEmpty());
}
Also http://www.geeksforgeeks.org/write-c-code-to-determine-if-two-trees-are-identical/
boolean
identicalTrees(Node a, Node b) {
/*1. both empty */
if
(a ==
null
&& b ==
null
) {
return
true
;
}
/* 2. both non-empty -> compare them */
if
(a !=
null
&& b !=
null
) {
return
(a.data == b.data
&& identicalTrees(a.left, b.left)
&& identicalTrees(a.right, b.right));
}
/* 3. one empty, one not -> false */
return
false
;
}
To identify if two trees are identical, we need to traverse both trees simultaneously, and while traversing we need to compare data and children of the trees.
The idea is to use level order traversal. We traverse both trees simultaneously and compare the data whenever we dequeue and item from queue.
Time complexity of above solution is O(n + m) where m and n are number of nodes in two trees.
bool
areIdentical(Node *root1, Node *root2)
{
// Return true if both trees are empty
if
(!root1 && !root2)
return
true
;
// Return false if one is empty and other is not
if
(!root1 || !root2)
return
false
;
// Create an empty queues for simultaneous traversals
queue<Node *> q1, q2;
// Enqueue Roots of trees in respective queues
q1.push(root1);
q2.push(root2);
while
(!q1.empty() && !q2.empty())
{
// Get front nodes and compare them
Node *n1 = q1.front();
Node *n2 = q2.front();
if
(n1->data != n2->data)
return
false
;
// Remove front nodes from queues
q1.pop(), q2.pop();
/* Enqueue left children of both nodes */
if
(n1->left && n2->left)
{
q1.push(n1->left);
q2.push(n2->left);
}
// If one left child is empty and other is not
else
if
(n1->left || n2->left)
return
false
;
// Right child code (Similar to left child code)
if
(n1->right && n2->right)
{
q1.push(n1->right);
q2.push(n2->right);
}
else
if
(n1->right || n2->right)
return
false
;
}
return
true
;
}
Read full article from Yu's Coding Garden : leetcode Question 90: Same Tree