**Symmetric Binary Tree - Amazon Top Interview Questions**

### Problem Statement :

Given the root to a binary tree root, return whether it is symmetric. Constraints n ≤ 100,000 where n is the number of nodes in root Example 1 Input root = [0, [2, [5, null, null], [1, null, null]], [2, [1, null, null], [5, null, null]]] Output True Example 2 Input root = [1, [2, null, null], [3, null, null]] Output False Example 3 Input root = [1, [2, null, null], null] Output False Example 4 Input root = [1, null, [2, null, null]] Output False

### Solution :

` ````
Solution in C++ :
bool solve(Tree* root) {
if (!root) return true;
stack<Tree*> p, q;
p.push(root->left), q.push(root->right);
while (!p.empty() && !q.empty()) {
auto m = p.top(), n = q.top();
p.pop(), q.pop();
if (!m && !n) continue;
else if (!m || !n) return false;
if (m->val != n->val)
return false;
p.push(m->left), p.push(m->right);
q.push(n->right), q.push(n->left);
}
return p.empty() && q.empty();
}
```

` ````
Solution in Java :
import java.util.*;
/**
* public class Tree {
* int val;
* Tree left;
* Tree right;
* }
*/
class Solution {
public boolean solve(Tree root) {
return versionA(root);
// return versionB(root);
}
// breadth first search, with padding for null child nodes
// performance: 62%
// This also works when Tree is non-bianry, i.e. more than 2 child nodes for a parent node.
boolean versionA(Tree root) {
if (root == null) {
return true;
}
// a special NULL node
Tree padding = new Tree();
ArrayList<Tree> list = new ArrayList<>();
list.add(root);
while (!list.isEmpty()) {
if (!isValid(list, padding)) {
return false;
}
list = getNextLevel(list, padding);
}
return true;
}
// check as if we were checking palindrome: all the nodes at one level
boolean isValid(ArrayList<Tree> list, Tree padding) {
int head = 0;
int tail = list.size() - 1;
while (head < tail) {
if (!isValid(list.get(head), list.get(tail), padding)) {
return false;
}
head++;
tail--;
}
return true;
}
boolean isValid(Tree a, Tree b, Tree padding) {
if ((a == padding) && (b == padding)) {
return true;
}
if ((a == padding) || (b == padding)) {
return false;
}
// no padding
return a.val == b.val;
}
// get all the nodes at next level, adding the spcial "padding" node when a child node is null.
ArrayList<Tree> getNextLevel(ArrayList<Tree> list, Tree padding) {
ArrayList<Tree> result = new ArrayList<>();
for (Tree node : list) {
if (node == padding) {
continue;
}
if (node.left != null) {
result.add(node.left);
} else {
result.add(padding);
}
if (node.right != null) {
result.add(node.right);
} else {
result.add(padding);
}
}
return result;
}
// this is how most people implemented. It is a better solution:
// shorter code, less memory and better performance
// performance: 85%
boolean versionB(Tree root) {
if (root == null) {
return true;
}
return recursionB(root.left, root.right);
}
boolean recursionB(Tree a, Tree b) {
if ((a == null) && (b == null)) {
return true;
}
if ((a == null) || (b == null)) {
return false;
}
if (a.val != b.val) {
return false;
}
return recursionB(a.left, b.right) && recursionB(a.right, b.left);
}
}
```

` ````
Solution in Python :
# class Tree:
# def __init__(self, val, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def solve(self, root):
def symmetry(l, r):
return (l is None and r is None) or bool(
l
and r
and l.val == r.val
and symmetry(l.left, r.right)
and symmetry(l.right, r.left)
)
return not root or symmetry(root.left, root.right)
```

## View More Similar Problems

## Tree: Huffman Decoding

Huffman coding assigns variable length codewords to fixed length input characters based on their frequencies. More frequent characters are assigned shorter codewords and less frequent characters are assigned longer codewords. All edges along the path to a character contain a code digit. If they are on the left side of the tree, they will be a 0 (zero). If on the right, they'll be a 1 (one). Only t

View Solution →## Binary Search Tree : Lowest Common Ancestor

You are given pointer to the root of the binary search tree and two values v1 and v2. You need to return the lowest common ancestor (LCA) of v1 and v2 in the binary search tree. In the diagram above, the lowest common ancestor of the nodes 4 and 6 is the node 3. Node 3 is the lowest node which has nodes and as descendants. Function Description Complete the function lca in the editor b

View Solution →## Swap Nodes [Algo]

A binary tree is a tree which is characterized by one of the following properties: It can be empty (null). It contains a root node only. It contains a root node with a left subtree, a right subtree, or both. These subtrees are also binary trees. In-order traversal is performed as Traverse the left subtree. Visit root. Traverse the right subtree. For this in-order traversal, start from

View Solution →## Kitty's Calculations on a Tree

Kitty has a tree, T , consisting of n nodes where each node is uniquely labeled from 1 to n . Her friend Alex gave her q sets, where each set contains k distinct nodes. Kitty needs to calculate the following expression on each set: where: { u ,v } denotes an unordered pair of nodes belonging to the set. dist(u , v) denotes the number of edges on the unique (shortest) path between nodes a

View Solution →## Is This a Binary Search Tree?

For the purposes of this challenge, we define a binary tree to be a binary search tree with the following ordering requirements: The data value of every node in a node's left subtree is less than the data value of that node. The data value of every node in a node's right subtree is greater than the data value of that node. Given the root node of a binary tree, can you determine if it's also a

View Solution →## Square-Ten Tree

The square-ten tree decomposition of an array is defined as follows: The lowest () level of the square-ten tree consists of single array elements in their natural order. The level (starting from ) of the square-ten tree consists of subsequent array subsegments of length in their natural order. Thus, the level contains subsegments of length , the level contains subsegments of length , the

View Solution →