LeetCode - Symmetric Tree
2014-01-16 01:51
281 查看
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
For example, this binary tree is symmetric:
But the following is not:
Note: Bonus points if you could solve it both recursively and iteratively.
http://oj.leetcode.com/problems/symmetric-tree/
Solution:
Recursive way is kind of like same tree. We can check if left subtree is mirror of right subtree, which is compare root1->left and root2->right, root1->right and root2->left.
Iterative way is to check every level. Attention that inorder traversal is not correct in that input {1, 2, 3, 3, #, 2, #}. The result is {3, 2, 1, 2, 3} but obviously it is not a symmetric tree.
https://github.com/starcroce/leetcode/blob/master/symmetric_tree.cpp
// 28 ms for 190 cases
/**
* 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) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
if(root == NULL) {
return true;
}
return isSym(root->left, root->right);
}
bool isSym(TreeNode *left, TreeNode *right) {
if(left == NULL && right == NULL) {
return true;
}
if(left == NULL || right == NULL) {
return false;
}
if(left->val != right->val) {
return false;
}
if(!isSym(left->right, right->left)) {
return false;
}
if(!isSym(left->left, right->right)) {
return false;
}
return true;
}
};
For example, this binary tree is symmetric:
1 / \ 2 2 / \ / \ 3 4 4 3
But the following is not:
1 / \ 2 2 \ \ 3 3
Note: Bonus points if you could solve it both recursively and iteratively.
http://oj.leetcode.com/problems/symmetric-tree/
Solution:
Recursive way is kind of like same tree. We can check if left subtree is mirror of right subtree, which is compare root1->left and root2->right, root1->right and root2->left.
Iterative way is to check every level. Attention that inorder traversal is not correct in that input {1, 2, 3, 3, #, 2, #}. The result is {3, 2, 1, 2, 3} but obviously it is not a symmetric tree.
https://github.com/starcroce/leetcode/blob/master/symmetric_tree.cpp
// 28 ms for 190 cases
/**
* 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) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
if(root == NULL) {
return true;
}
return isSym(root->left, root->right);
}
bool isSym(TreeNode *left, TreeNode *right) {
if(left == NULL && right == NULL) {
return true;
}
if(left == NULL || right == NULL) {
return false;
}
if(left->val != right->val) {
return false;
}
if(!isSym(left->right, right->left)) {
return false;
}
if(!isSym(left->left, right->right)) {
return false;
}
return true;
}
};
// 48 ms for 191 test cases // iterative way, check every level /** * 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) { // IMPORTANT: Please reset any member data you declared, as // the same Solution instance will be reused for each test case. if(root == NULL) { return true; } vector<TreeNode *> preLevel; preLevel.push_back(root); while(!preLevel.empty()) { vector<TreeNode *> curLevel; // push child node to curLevel while(!preLevel.empty()) { TreeNode *tmp = preLevel.back(); preLevel.pop_back(); if(tmp == NULL) { continue; } curLevel.push_back(tmp->left); curLevel.push_back(tmp->right); } int start = 0, end = curLevel.size() - 1; // check the val in curLevel while(start < end) { TreeNode *leftNode = curLevel[start]; TreeNode *rightNode = curLevel[end]; int leftVal, rightVal; if(leftNode == NULL) { leftVal = -1; } else { leftVal = leftNode->val; } if(rightNode == NULL) { rightVal = -1; } else { rightVal = rightNode->val; } if(leftVal != rightVal) { return false; } start++; end--; } // replace preLevel with curLevel preLevel = curLevel; } return true; } };
相关文章推荐
- 【LeetCode-面试算法经典-Java实现】【101-Symmetric Tree(对称树)】
- LeetCode101 Symmetric Tree
- [LeetCode] Symmetric Tree
- [leetcode] Symmetric Tree
- leetcode 之 Symmetric Tree
- leetcode-Symmetric Tree 对称树
- LEETCODE--Symmetric Tree
- LeetCode101 Symmetric Tree
- LeetCode-101-Symmetric Tree(判断是否为对称树)
- leetcode 101 Symmetric Tree
- leetcode(24).101. Symmetric Tree
- LeetCode-Symmetric Tree
- LeetCode:Symmetric Tree
- LeetCode - Symmetric Tree
- LeetCode 101 Symmetric Tree
- [LeetCode#101]Symmetric Tree
- Leetcode-101. Symmetric Tree
- LeetCode:Symmetric Tree
- LeetCode 51 Symmetric Tree
- (Symmetric Tree--LeetCode)判断树的对称性