敢不敢把问题理清楚点???懒,写的啥破玩意儿,,,,回来继续写
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
/*
1.判断left 如果不等于right就错
2.分治
3.什么遍历方法?
*/
class Solution {
TreeNode* travl_left(TreeNode* root){
TreeNode* tmp=NULL;
tmp=root->left;
printf("%d\n",tmp->val);
return tmp;
}
TreeNode* travl_right(TreeNode* root){
TreeNode* tmp=NULL;
tmp=root->right;
printf("%d\n",tmp->val);
return tmp;
}
void equel(TreeNode* root){
if(root!=NULL){
int l = travl_left(root->left)->val;
int r = travl_right(root->right)->val;
if(l!=r) return false;
return true;
}
}
public:
bool isSymmetric(TreeNode* root) {
}
};
1.问题分析:
对称意味着出现偶数次,(非必要条件)
一层一层扫比较保险。每到一层就切换成两路
动态2的n次方,,,怎么写?
放弃治疗:看大佬答案去。
别人家的代码,,,
CPP Iterative and Recursive Solutions (Easy to Understand):
https://leetcode.com/problems/symmetric-tree/discuss/146961/Both-Iterative-and-Recursive-Solutions
131
December 4, 2018 3:32 PM
Read More
Bonus point
Iterative:
/**
* Definition for a binary tree node.
* 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;
stack<pair<TreeNode*, TreeNode*>> stck;
stck.push(make_pair(root->left, root->right));
while (!stck.empty()) {
auto p=stck.top(); stck.pop();
if (p.first == NULL && p.second == NULL) {
continue;
}
if (p.first == NULL || p.second == NULL) {
return false;
}
if (p.first->val != p.second->val) {
return false;
}
stck.push(make_pair(p.first->right, p.second->left));
stck.push(make_pair(p.first->left, p.second->right));
}
return true;
}
};
Recursive:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
bool helper(TreeNode* p, TreeNode* q) {
if (!p && !q) {
return true;
} else if (!p || !q) {
return false;
}
if (p->val != q->val) {
return false;
}
return helper(p->left,q->right) && helper(p->right, q->left);
}
public:
bool isSymmetric(TreeNode *root) {
if (!root) return true;
return helper(root->left, root->right);
}
};
572
July 8, 2018 4:04 PM
Read More
class Solution {
public:
bool isSymmetric(TreeNode* root) {
return isMirrorIterative(root);
//return isMirrorR(root,root);
}
bool isMirrorR(TreeNode* t1, TreeNode* t2) {
if (!t1 && !t2) return true;
if (!t1 || !t2) return false;
return (t1->val == t2->val) && isMirrorR(t1->right, t2->left) && isMirrorR(t1->left, t2->right);
}
bool isMirrorIterative(TreeNode *root) {
if (root == NULL) return true;
queue<TreeNode*> q1, q2;
q1.push(root->left);
q2.push(root->right);
while (!q1.empty() && !q2.empty())
{
TreeNode *left = q1.front(); q1.pop();
TreeNode *right = q2.front(); q2.pop();
if (!left && !right) continue;
if (!left || !right) return false;
if (left->val != right->val) return false;
q1.push(left->left);
q1.push(left->right);
q2.push(right->right);
q2.push(right->left);
}
return true;
}
};
补一下queue 和stack