s*e
2 楼
现在官网上有了..刚帮国内的姐妹定了一个...准备夏天回国带给她.
c*e
3 楼
所有recursive的题都能用stack做 你要熟悉啊
tripadvisor考到过
/**
* 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) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if(root==NULL)return true;
if(root->left==NULL||root->right==NULL){
if(root->left==NULL&&root->right==NULL)return true;
return false;
}
stack s1,s2;
s1.push(root->left);
s2.push(root->right);
while(!s1.empty()&&!s2.empty()){
TreeNode *n1=s1.top(),*n2=s2.top();
s1.pop();s2.pop();
if(n1->val!=n2->val)return false;
if(n1->left!=NULL&&n2->right!=NULL){
s1.push(n1->left);
s2.push(n2->right);
}
else if(n1->left!=NULL||n2->right!=NULL)
return false;
if(n1->right!=NULL&&n2->left!=NULL){
s1.push(n1->right);
s2.push(n2->left);
}
else if(n1->right!=NULL||n2->left!=NULL)
return false;
}
return true;
}
};
tripadvisor考到过
/**
* 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) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if(root==NULL)return true;
if(root->left==NULL||root->right==NULL){
if(root->left==NULL&&root->right==NULL)return true;
return false;
}
stack
s1.push(root->left);
s2.push(root->right);
while(!s1.empty()&&!s2.empty()){
TreeNode *n1=s1.top(),*n2=s2.top();
s1.pop();s2.pop();
if(n1->val!=n2->val)return false;
if(n1->left!=NULL&&n2->right!=NULL){
s1.push(n1->left);
s2.push(n2->right);
}
else if(n1->left!=NULL||n2->right!=NULL)
return false;
if(n1->right!=NULL&&n2->left!=NULL){
s1.push(n1->right);
s2.push(n2->left);
}
else if(n1->right!=NULL||n2->left!=NULL)
return false;
}
return true;
}
};
p*y
4 楼
我一会儿长草一会儿拔草来来去去好几回了
忍住了不买
UD家的盘太多了,也不缺这一个
忍住了不买
UD家的盘太多了,也不缺这一个
j*0
6 楼
我用的bst,思想就是左子树放一个队列右子树以反方向放一个队列,然后从队列中抽
出脑袋的时候比比。
public boolean isSymmetric(TreeNode root) {
if (root == null) {
return true;
}
Queue left_q = new LinkedList();
Queue right_q = new LinkedList();
left_q.offer(root.left);
right_q.offer(root.right);
while (!left_q.isEmpty() && !right_q.isEmpty()) {
TreeNode left = left_q.poll();
TreeNode right = right_q.poll();
if (left == null && right == null) {
continue;
} else if (left == null || right == null) {
return false;
} else {
if (left.val != right.val) {
return false;
}
left_q.offer(left.left);
left_q.offer(left.right);
right_q.offer(right.right);
right_q.offer(right.left);
}
}
return true;
}
出脑袋的时候比比。
public boolean isSymmetric(TreeNode root) {
if (root == null) {
return true;
}
Queue
Queue
left_q.offer(root.left);
right_q.offer(root.right);
while (!left_q.isEmpty() && !right_q.isEmpty()) {
TreeNode left = left_q.poll();
TreeNode right = right_q.poll();
if (left == null && right == null) {
continue;
} else if (left == null || right == null) {
return false;
} else {
if (left.val != right.val) {
return false;
}
left_q.offer(left.left);
left_q.offer(left.right);
right_q.offer(right.right);
right_q.offer(right.left);
}
}
return true;
}
k*a
7 楼
你们两个都很强大!
前者用的c++,不知道后面那个用的什么语言啊?
前者用的c++,不知道后面那个用的什么语言啊?
I*s
9 楼
也用stack. 简洁一些,也能过。
bool isSymmetric3(TreeNode * root) {
if (! root) return true;
stack s1, s2;
s1.push(root->left);
s2.push(root->right);
while(! s1.empty() && ! s2.empty()) {
TreeNode * n1 = s1.top(); s1.pop();
TreeNode * n2 = s2.top(); s2.pop();
if (!n1 && !n2) continue;
if (!n1 || !n2 || n1->val != n2->val) return false;
s1.push(n1->left);
s1.push(n1->right);
s2.push(n2->right);
s2.push(n2->left);
}
return true;
}
bool isSymmetric3(TreeNode * root) {
if (! root) return true;
stack
s1.push(root->left);
s2.push(root->right);
while(! s1.empty() && ! s2.empty()) {
TreeNode * n1 = s1.top(); s1.pop();
TreeNode * n2 = s2.top(); s2.pop();
if (!n1 && !n2) continue;
if (!n1 || !n2 || n1->val != n2->val) return false;
s1.push(n1->left);
s1.push(n1->right);
s2.push(n2->right);
s2.push(n2->left);
}
return true;
}
I*s
10 楼
完全相同的结构,改用queue, 也能过:
bool isSymmetric(TreeNode * root) {
if (! root) return true;
queue s1, s2;
s1.push(root->left);
s2.push(root->right);
while(! s1.empty() && ! s2.empty()) {
TreeNode * n1 = s1.front(); s1.pop();
TreeNode * n2 = s2.front(); s2.pop();
if (!n1 && !n2) continue;
if (!n1 || !n2 || n1->val != n2->val) return false;
s1.push(n1->left);
s1.push(n1->right);
s2.push(n2->right);
s2.push(n2->left);
}
return true;
}
bool isSymmetric(TreeNode * root) {
if (! root) return true;
queue
s1.push(root->left);
s2.push(root->right);
while(! s1.empty() && ! s2.empty()) {
TreeNode * n1 = s1.front(); s1.pop();
TreeNode * n2 = s2.front(); s2.pop();
if (!n1 && !n2) continue;
if (!n1 || !n2 || n1->val != n2->val) return false;
s1.push(n1->left);
s1.push(n1->right);
s2.push(n2->right);
s2.push(n2->left);
}
return true;
}
相关阅读
忍不住奔真人秀皮包脏了大家都用什么清洗呢Have one $15 off $50EL 的ANR怎么买便宜呀?求个sephora的code,有6pm 20%off可换求推荐卷发器有人在ebay上买过UGGs么?奔熊猫套装刚到手的Kate SpadeGNC $10 off $75 code December Eff求sephora code,包子,blooming的10%off无限制的code,加25 off100这个好不好看?台湾美女阿喜新作:女生為啥一直喊減肥卻一直減不了 (转载)beauty.com $10 off your $40 code发个sephora-满50-15请问大家都涂什么保护皮革林丹真有型jms帮忙看看这个玉镯子值多少钱?有人有像我一样倒霉吗? jms bless 一下吧。。。包子求sephora 15 off / 20 off coupon