Leetcode: Invalid Ric Tree
I. Question
Give a binary tree and determine whether the tree is mirrored (symmetric ).
Example: 1
/\
2 2
/\/\
3 4 4 3
But this is not: 1
/\
2 2
\\
3 3
Ii. Analysis
1. Recursion
Start from the root and check whether the left and right subtree are symmetric by iteration.
The symmetric condition of the left and right subtree is:
1> the values of the two nodes are equal.
2> the left subtree of the Left node is symmetric with the right subtree of the right node.
3> the right subtree of the Left node is symmetric with the left subtree of the right Node
2. Non-recursion
When non-recursion is used, we can create two queues, and queue the left and right subtree of the root node separately. When we compare the queues and find that the node values in the symmetric positions are not equal, false is returned;
1> queue the left and right subtree of the Root Node
2> queue
3> If the value is equal, 4 is returned. Otherwise, false is returned.
4> queue the left subtree of the current left node and the right subtree of the current right node. If either of them is empty, false is returned.
5> queue the left subtree of the current right node and the right subtree of the current left node. If one of them is empty, false is returned.
6> finally, if the two queues are empty at the same time, true is returned. Otherwise, false is returned.
Recursion:
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public:bool isSymmetric(TreeNode *Lroot,TreeNode *Rroot) { if(Lroot==NULL) return Rroot==NULL; else { if(Rroot==NULL) return false; if(Lroot->val!=Rroot->val) return false; if(!isSymmetric(Lroot->right,Rroot->left)) return false; if(!isSymmetric(Lroot->left,Rroot->right)) return false; return true; }} bool isSymmetric(TreeNode *root) { if(root==NULL) return true; TreeNode *Rroot; TreeNode *Lroot; Rroot = root->right; Lroot = root->left;return isSymmetric(Lroot,Rroot); }};
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public:bool isSymmetric(TreeNode *root) { if (! root) return true; return compare(root->left, root->right); } bool compare(TreeNode *Lroot, TreeNode *Rroot) { if (Lroot!=NULL && Rroot!=NULL) return true; if (Lroot!=NULL && Rroot==NULL || Lroot==NULL && Rroot!=NULL) return false; if (Lroot->val != Rroot->val) return false; return compare(Lroot->left, Rroot->right) && compare(Lroot->right, Rroot->left); }};
Non-recursion:
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public: bool isSymmetric(TreeNode *root) { if(root==NULL) return true; queue
Lque; queue
Rque; if(root->left) Lque.push(root->left); if(root->right) Rque.push(root->right); while(!Lque.empty()&&!Rque.empty()){ TreeNode *ql = Lque.front(); TreeNode *qr = Rque.front(); Lque.pop(); Rque.pop(); if(ql->val == qr->val){ if(ql->left&&qr->right){ Lque.push(ql->left); Rque.push(qr->right); } else if(ql->left||qr->right) return false; if(qr->left&&ql->right){ Lque.push(qr->left); Rque.push(ql->right); } else if(qr->left||ql->right) return false; } else return false; } if(Lque.empty() && Rque.empty()) return true; else return false; }};