|
| 1 | +Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). |
| 2 | + |
| 3 | +For example, this binary tree [1,2,2,3,4,4,3] is symmetric: |
| 4 | + |
| 5 | + 1 |
| 6 | + / \ |
| 7 | + 2 2 |
| 8 | + / \ / \ |
| 9 | +3 4 4 3 |
| 10 | + |
| 11 | + |
| 12 | +But the following [1,2,2,null,3,null,3] is not: |
| 13 | + |
| 14 | + 1 |
| 15 | + / \ |
| 16 | + 2 2 |
| 17 | + \ \ |
| 18 | + 3 3 |
| 19 | + |
| 20 | + |
| 21 | +Follow up: Solve it both recursively and iteratively. |
| 22 | + |
| 23 | + |
| 24 | + |
| 25 | + |
| 26 | + |
| 27 | + |
| 28 | + |
| 29 | + |
| 30 | +/** |
| 31 | + * Definition for a binary tree node. |
| 32 | + * struct TreeNode { |
| 33 | + * int val; |
| 34 | + * TreeNode *left; |
| 35 | + * TreeNode *right; |
| 36 | + * TreeNode() : val(0), left(nullptr), right(nullptr) {} |
| 37 | + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} |
| 38 | + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} |
| 39 | + * }; |
| 40 | + */ |
| 41 | +class Solution { |
| 42 | +public: |
| 43 | + |
| 44 | + bool isSymmetricUtil(TreeNode *root1, TreeNode *root2){ |
| 45 | + if(root1==NULL && root2==NULL) return true; |
| 46 | + if(root1==NULL || root2==NULL) return false; |
| 47 | + |
| 48 | + return (root1->val==root2->val && isSymmetricUtil(root1->left, root2->right) && isSymmetricUtil(root1->right, root2->left)); |
| 49 | + } |
| 50 | + |
| 51 | + bool isSymmetric(TreeNode* root) { |
| 52 | + TreeNode *root1=root, *root2=root; |
| 53 | + return isSymmetricUtil(root1, root2); |
| 54 | + } |
| 55 | +}; |
| 56 | + |
| 57 | + |
| 58 | + |
| 59 | + |
| 60 | +/* |
| 61 | +For two trees to be mirror images, the following three conditions must be true |
| 62 | +1 - Their root node's key must be same |
| 63 | +2 - left subtree of left tree and right subtree of right tree have to be mirror images |
| 64 | +3 - right subtree of left tree and left subtree of right tree have to be mirror images |
| 65 | +*/ |
| 66 | + |
| 67 | + |
| 68 | + |
| 69 | + |
| 70 | + |
| 71 | +// Iterative |
| 72 | + |
| 73 | + |
| 74 | +/** |
| 75 | + * Definition for a binary tree node. |
| 76 | + * struct TreeNode { |
| 77 | + * int val; |
| 78 | + * TreeNode *left; |
| 79 | + * TreeNode *right; |
| 80 | + * TreeNode() : val(0), left(nullptr), right(nullptr) {} |
| 81 | + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} |
| 82 | + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} |
| 83 | + * }; |
| 84 | + */ |
| 85 | +class Solution { |
| 86 | +public: |
| 87 | + bool isSymmetric(TreeNode* root) { |
| 88 | + if(!root) return true; |
| 89 | + if(!root->left && !root->right) return true; |
| 90 | + queue<TreeNode*> q; |
| 91 | + |
| 92 | + // Add root to queue two times so that |
| 93 | + // it can be checked if either one child |
| 94 | + // alone is NULL or not. |
| 95 | + q.push(root); |
| 96 | + q.push(root); |
| 97 | + |
| 98 | + while(!q.empty()){ |
| 99 | + TreeNode *leftNode=q.front(); |
| 100 | + q.pop(); |
| 101 | + TreeNode *rightNode=q.front(); |
| 102 | + q.pop(); |
| 103 | + |
| 104 | + if(leftNode->val!=rightNode->val) return false; |
| 105 | + |
| 106 | + // Push left child of left subtree node |
| 107 | + // and right child of right subtree node in queue. |
| 108 | + if(leftNode->left && rightNode->right){ |
| 109 | + q.push(leftNode->left); |
| 110 | + q.push(rightNode->right); |
| 111 | + } |
| 112 | + // If only one child is present alone |
| 113 | + // and other is NULL, then tree is not symmetric. |
| 114 | + else if(leftNode->left || rightNode->right){ |
| 115 | + return false; |
| 116 | + } |
| 117 | + |
| 118 | + if(leftNode->right && rightNode->left){ |
| 119 | + q.push(leftNode->right); |
| 120 | + q.push(rightNode->left); |
| 121 | + } |
| 122 | + else if(leftNode->right || rightNode->left){ |
| 123 | + return false; |
| 124 | + } |
| 125 | + } |
| 126 | + return true; |
| 127 | + } |
| 128 | +}; |
| 129 | + |
| 130 | + |
| 131 | +/* |
| 132 | +Note that for a symmetric, the elements at every level are palindromic. |
| 133 | +In other words, |
| 134 | +1. The left child of left subtree = right child of right subtree. |
| 135 | +2. The right child of left subtree = left child of right subtree. |
| 136 | +If we insert the left child of left subtree first followed by right child of the right subtree in the queue, |
| 137 | +we only need to ensure that these are equal. |
| 138 | +Similarly, If we insert the right child of left subtree followed by left child of the right subtree in the queue, |
| 139 | +we again need to ensure that these are equal. |
| 140 | +*/ |
0 commit comments