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;
}
相关阅读
有没有一种奔照片的方式,能让人看出好看程度,但认不出是谁?大家上班都用什么包包?脸上出油经常流到眼睛里,很疼是Hemez吧? nasty Lindsay wow彭大将军今日最新出炉,请挑刺!!!!亲爱的Her Highness,请问您还available吗?哪能买到便宜的BOTTEGA VENETA 钱包外交部麻烦也给孙宁同学定做套西装吧 (转载)昨天睡得很早,可是黑眼圈更黑了~~~~~求推荐一款妆前底乳啊~~~~~头皮屑,头皮屑, 求助求推荐眉粉~~~~~~~~~~大家来测测自己的音乐天赋吧 (转载)美女打呵欠是这样的关于脸上疙瘩的问题奶茶MM展现制服诱惑 这才叫清纯妩媚混合体啊! (转载)真丝蓝衬衫如何搭配习夫人到花生版潜水后,立马把袖子剪短了请教,现在coach包都不给防尘袋了吗?看看咱们的领袖在威尼斯