LeetCode101.Symmetric Tree
题目
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
For example, this binary tree [1,2,2,3,4,4,3]
is symmetric:
1 / \ 2 2 / \ / \ 3 4 4 3
But the following [1,2,2,null,3,null,3]
is not:
1 / \ 2 2 \ \ 3 3
Note:
Bonus points if you could solve it both recursively and iteratively.
题解
题目的意思是,判断一棵二叉树是不是自我镜像的,也就是以中心为轴镜面对称。解题关键是确定镜面节点对,然后判断该对节点值是否一样即可。通过观察可以发现,只要左子树与右子树镜面对称即可,左子树的左孩子与右子树的右孩子配对,左子树的右孩子与右子树的左孩子配对,因而可以递归求解 也可以迭代求解。
(1)递归函数的输入是配对节点,输出是这两个点的值是否相同。如果不相同则返回False,否则需判断这两个节点的左右孩子是否相同。
1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 */ 10 class Solution { 11 public: 12 bool isSymmetric(TreeNode* root) { 13 if(root == NULL) 14 return true; 15 return isSymmetric(root->left,root->right); 16 } 17 bool isSymmetric(TreeNode* left,TreeNode* right) 18 { 19 if(left == NULL && right == NULL) return true; 20 if(left == NULL || right == NULL) return false; 21 if(left->val != right->val) 22 return false; 23 return isSymmetric(left->left,right->right) && isSymmetric(left->right,right->left); 24 } 25 };
(2)迭代解法:借助两个队列,一个代表左子树,一个代表右子树,队列中节点以此对应。
1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 */ 10 class Solution { 11 public: 12 bool isSymmetric(TreeNode* root) { 13 if(root == NULL) 14 return true; 15 queue<TreeNode*> lq,rq; 16 lq.push(root->left); 17 rq.push(root->right); 18 while(!lq.empty() && !rq.empty()){ 19 TreeNode *tempLeft = lq.front(), * tempRight = rq.front(); 20 lq.pop();rq.pop(); 21 if(tempLeft == NULL){ 22 if(tempRight == NULL) 23 continue; 24 else 25 return false; 26 } 27 if(tempRight == NULL) 28 return false; 29 if(tempLeft -> val != tempRight -> val) 30 return false; 31 lq.push(tempLeft->left);rq.push(tempRight->right); 32 lq.push(tempLeft->right);rq.push(tempRight->left); 33 } 34 if(!lq.empty()) 35 return false; 36 if(!rq.empty()) 37 return false; 38 return true; 39 } 40 41 };
迭代的时间居然比递归的时间慢了一倍。。用一个队列也是可以模拟的,每次从队首取出两个相互配对的节点即可。
1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 */ 10 class Solution { 11 public: 12 bool isSymmetric(TreeNode* root) { 13 if(root == NULL) 14 return true; 15 queue<TreeNode*> q; 16 q.push(root->left); 17 q.push(root->right); 18 while(!q.empty()){ 19 TreeNode *tempLeft = q.front(); q.pop(); 20 TreeNode *tempRight = q.front(); q.pop(); 21 if(tempLeft == NULL && tempRight == NULL) continue; 22 if(tempLeft == NULL || tempRight == NULL) return false; 23 if(tempLeft -> val != tempRight -> val) return false; 24 q.push(tempLeft->left); 25 q.push(tempRight->right); 26 q.push(tempLeft->right); 27 q.push(tempRight->left); 28 } 29 return true; 30 } 31 32 };
至于时间上变慢的原因,目前不知。。