105. Check for symmetrical BTs

Given the root of a binary tree, check whether it is a mirror of itself (i.e., symmetric around its center).

Example 1:

Input : root = [1, 2, 2, 3, 4, 4, 3]

Output : true

Explanation :

Example 2:

Input : root = [1, 2, 2, null, 3, null, 3]

Output : false

Explanation : When a straight line is drawn through the root node and the tree is folded around it, the rightmost node 3 is overlapped with null node and the node 3 present at left of root node is overlapped with null nodes.

So both node 3 in tree does not show symmetric behaviour.

Now Your Turn!

Pick the correct output for the given input

Input: root = [1, 2, 3]

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 1<= Number of Nodes <= 104
  • -100 <= Node.val <= 100

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

0
/**
* Definition for a binary tree node.
* struct TreeNode {
* int data;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int val) : data(val) , left(nullptr) , right(nullptr) {}
* };
**/
 
class Solution {
public:
bool isSymmetric(TreeNode* root) {
//your code goes here
}
};
Test Case

Input:

Root