Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
For example, this binary tree is symmetric:
For example, this binary tree is symmetric:
1 / \ 2 2 / \ / \ 3 4 4 3But the following is not:
1 / \ 2 2 \ \ 3 3
private boolean isSymmetric(TreeNode left, TreeNode right) {
if (left == null && right == null) return true;
if (left == null || right == null || left.val != right.val) return false;
return isSymmetric(left.left, right.right) && isSymmetric(left.right, right.left);
}
public boolean isSymmetric(TreeNode root) {
return (root == null) || isSymmetric(root.left, root.right);
}
http://www.programcreek.com/2014/03/leetcode-symmetric-tree-java/public boolean isSymmetric(TreeNode root) { if (root == null) return true; return isSymmetric(root.left, root.right); } public boolean isSymmetric(TreeNode l, TreeNode r) { if (l == null && r == null) { return true; } else if (r == null || l == null) { return false; } if (l.val != r.val) return false; if (!isSymmetric(l.left, r.right)) return false; if (!isSymmetric(l.right, r.left)) return false; return true; }
https://xuezhashuati.blogspot.com/2017/05/101-symmetric-tree.html
Method 1: recursive
We recursively check if the subtrees are symmetric.
Given p and q, to check if p and q are symmetric trees, we check:
1. p.val == q.val
2. p.left and p.right are symmetric trees
3. p.right and q.left are symmetric trees
Method 1: recursive
We recursively check if the subtrees are symmetric.
Given p and q, to check if p and q are symmetric trees, we check:
1. p.val == q.val
2. p.left and p.right are symmetric trees
3. p.right and q.left are symmetric trees
public boolean isSymmetric(TreeNode root) { if (root == null) { return true; } return isMirror(root.left, root.right); } public boolean isMirror(TreeNode p, TreeNode q) { if (p == null && q == null) { return true; } if (p == null || q == null) { return false; } return p.val == q.val && isMirror(p.left, q.right) && isMirror(p.right, q.left); }https://leetcode.com/discuss/23753/versions-java-recursion-optimized-recursion-order-iteration
There are two kinds of iteration at least. The BFS-like iteration, which is based on queue, has a space complexity of O(n). And the DFS-like iteration, which is based on stack, has a better space complexity of O(log n).
X. Using one queue
public boolean isSymmetric(TreeNode root) {
Deque<TreeNode[]> stack = new LinkedList<>();
stack.push(new TreeNode[]{root, root});
while (!stack.isEmpty()) {
TreeNode[] pair = stack.pop();
TreeNode t0 = pair[0], t1 = pair[1];
if (t0 == null && t1 == null) {
continue;
}
if (t0 == null || t1 == null || t0.val != t1.val) {
return false;
}
stack.push(new TreeNode[]{t0.left, t1.right});
stack.push(new TreeNode[]{t0.right, t1.left});
}
return true;
}https://leetcode.com/discuss/41807/short-and-clean-java-iterative-solution
public boolean isSymmetric(TreeNode root) { Queue<TreeNode> q = new LinkedList<TreeNode>(); if(root == null) return true; q.add(root.left); q.add(root.right); while(q.size() > 1){ TreeNode left = q.poll(), right = q.poll(); if(left== null&& right == null) continue; if(left == null ^ right == null) return false; if(left.val != right.val) return false; q.add(left.left); q.add(right.right); q.add(left.right); q.add(right.left); } return true; }
X. Using two queues
https://leetcode.com/discuss/81947/java-iterative-%26-recursive-solutions
public boolean isSymmetric(TreeNode root) { if(root==null) return true; Queue<TreeNode> q1=new LinkedList<>(), q2=new LinkedList<>(); q1.add(root.left); q2.add(root.right); while(!q1.isEmpty() && !q2.isEmpty()) { int size1=q1.size(), size2=q2.size(); if(size1!=size2) return false; for(int i=0; i<size1; i++) { TreeNode current1=q1.remove(), current2=q2.remove(); if(current1==null && current2==null) continue; if(current1==null || current2==null) return false; if(current1.val!=current2.val) return false; q1.add(current1.left); q1.add(current1.right); q2.add(current2.right); q2.add(current2.left); } } return q1.isEmpty() && q2.isEmpty(); }
private boolean isSymmetric(TreeNode left, TreeNode right) {
if (left == null && right == null) return true;
if (left == null || right == null || left.val != right.val) return false;
Queue<TreeNode> lque = new ArrayDeque<TreeNode>();
Queue<TreeNode> rque = new ArrayDeque<TreeNode>();
lque.add(left); rque.add(right);
while (!lque.isEmpty() && !rque.isEmpty()) {
TreeNode l = lque.remove();
TreeNode r = rque.remove();
if (l.left != null && r.right != null && l.left.val == r.right.val) {
lque.add(l.left); rque.add(r.right);
} else if (!(l.left == null && r.right == null)) {
return false;
}
if (l.right != null && r.left != null && l.right.val == r.left.val) {
lque.add(l.right); rque.add(r.left);
} else if (!(l.right == null && r.left == null)) {
return false;
}
}
return (lque.isEmpty() && rque.isEmpty());
}
public boolean isSymmetric(TreeNode root) {
return (root == null ) || isSymmetric(root.left, root.right);
}
From http://rangercyh.blog.51cto.com/1444712/1306485From http://gongxuns.blogspot.com/2012/12/leetcodesymmetric-tree.html
public boolean isSymmetric(TreeNode root) { if(root==null) return true; LinkedList<TreeNode> l = new LinkedList<TreeNode>(), r = new LinkedList<TreeNode>(); l.add(root.left); r.add(root.right); while(!l.isEmpty() && !r.isEmpty()){ TreeNode temp1=l.poll(), temp2=r.poll(); if(temp1==null && temp2!=null || temp1!=null && temp2==null) return false; if(temp1!=null){ if(temp1.val!=temp2.val) return false; l.add(temp1.left); l.add(temp1.right); r.add(temp2.right); r.add(temp2.left); } } return true; }X. Use Stack
The ideas is to use stack to store nodes from two trees. And every time we pop two of them and compare the value.
When we push, we push the left node of p and right node of q, or we push the right node of p and left node of q.
The nodes are pushed into the stack in pairs, so if the size of stack mod 2 is not zero, it must not be symmetric.
When we push, we push the left node of p and right node of q, or we push the right node of p and left node of q.
The nodes are pushed into the stack in pairs, so if the size of stack mod 2 is not zero, it must not be symmetric.
public boolean isSymmetric(TreeNode root) { if (root == null) { return true; } Stack<TreeNode> stack = new Stack<>(); if (!addNode(root.left, root.right, stack)) { return false; } while (!stack.isEmpty()) { if (stack.size() % 2 != 0) { return false; } TreeNode p = stack.pop(); TreeNode q = stack.pop(); if (p.val != q.val) { return false; } if (!addNode(p.left, q.right, stack)) { return false; } if (!addNode(p.right, q.left, stack)) { return false; } } return true; } public boolean addNode(TreeNode p, TreeNode q, Stack<TreeNode> stack) { if (p == null && q == null) { return true; } if (p == null || q == null) { return false; } stack.push(p); stack.push(q); return true; }https://leetcode.com/discuss/19859/slim-java-solution
The idea is: 1. level traversal. 2. push nodes onto stack, every 2 consecutive is a pair, and should either be both null or have equal value. repeat until stack is empty.
public boolean isSymmetric(TreeNode root) { if (root == null) return true; Stack<TreeNode> stack = new Stack<TreeNode>(); stack.push(root.left); stack.push(root.right); while (!stack.isEmpty()) { TreeNode node1 = stack.pop(); TreeNode node2 = stack.pop(); if (node1 == null && node2 == null) continue; if (node1 == null || node2 == null) return false; if (node1.val != node2.val) return false; stack.push(node1.left); stack.push(node2.right); stack.push(node1.right); stack.push(node2.left); } return true; }
https://leetcode.com/discuss/18354/recursive-and-non-recursive-solutions-in-java
Recursive Version:
bool isSymmetric(TreeNode *root)
{
if (!root) return true;
return isSymmetric(root->left, root->right);
}
bool isSymmetric(TreeNode *lt, TreeNode *rt)
{
if (!lt && !rt) return true;
if (lt && !rt || !lt && rt || lt->val != rt->val) return false;
return isSymmetric(lt->left, rt->right) &&isSymmetric(lt->right, rt->left);
}
http://www.geeksforgeeks.org/symmetric-tree-tree-which-is-mirror-image-of-itself/
Also refer to: http://rangercyh.blog.51cto.com/1444712/1306485
Read full article from Yu's Coding Garden : leetcode Question 109: Symmetric Tree
public boolean isSymmetric(TreeNode root) {
if(root==null) return true;
Stack<TreeNode> stack = new Stack<TreeNode>();
TreeNode left, right;
if(root.left!=null){
if(root.right==null) return false;
stack.push(root.left);
stack.push(root.right);
}
else if(root.right!=null){
return false;
}
while(!stack.empty()){
if(stack.size()%2!=0) return false;
right = stack.pop();
left = stack.pop();
if(right.val!=left.val) return false;
if(left.left!=null){
if(right.right==null) return false;
stack.push(left.left);
stack.push(right.right);
}
else if(right.right!=null){
return false;
}
if(left.right!=null){
if(right.left==null) return false;
stack.push(left.right);
stack.push(right.left);
}
else if(right.left!=null){
return false;
}
}
return true;
}
Recursive Version:
bool isSymmetric(TreeNode *root)
{
if (!root) return true;
return isSymmetric(root->left, root->right);
}
bool isSymmetric(TreeNode *lt, TreeNode *rt)
{
if (!lt && !rt) return true;
if (lt && !rt || !lt && rt || lt->val != rt->val) return false;
return isSymmetric(lt->left, rt->right) &&isSymmetric(lt->right, rt->left);
}
http://www.geeksforgeeks.org/symmetric-tree-tree-which-is-mirror-image-of-itself/
bool
isMirror(
struct
Node *root1,
struct
Node *root2)
{
// If both trees are emptu, then they are mirror images
if
(root1 == NULL && root2 == NULL)
return
true
;
// For two trees to be mirror images, the following three
// conditions must be true
// 1 - Their root node's key must be same
// 2 - left subtree of left tree and right subtree
// of right tree have to be mirror images
// 3 - right subtree of left tree and left subtree
// of right tree have to be mirror images
if
(root1 && root2 && root1->key == root2->key)
return
isMirror(root1->left, root2->right) &&
isMirror(root1->right, root2->left);
// if neither of above conditions is true then root1
// and root2 are not mirror images
return
false
;
}
// Returns true if a tree is symmetric i.e. mirror image of itself
bool
isSymmetric(
struct
Node* root)
{
// Check if tre is mirror of itself
return
isMirror(root, root);
}
Read full article from Yu's Coding Garden : leetcode Question 109: Symmetric Tree