Leetcode 101. Symmetric Tree
2017-02-03 08:08
465 查看
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:
But the following [1,2,2,null,3,null,3] is not:
Note:
Bonus points if you could solve it both recursively and iteratively.
s思路:
1. 树,选用那种遍历方式最快?要比较是否左右镜像对称。初看,想到用deque的bfs,把每层的数先全部放入deque,然后每次从两端取指针,看是否指针要么都null,要么都有、且数字相等。用deque还是麻烦,又想到可以用两个queue就可以解决问题,对root的左子树所有节点用一个queue来装,右子树用另一个queue来装。左子树每一层从左往右装,右子树每一层从右往左装,分别读出来比较即可!!妙啊!
2. 这个用两个queue的方法,应该是思维上受到上一题比较两个树是否相等的方法启发!
2. 再思考,还可以recursive. 用两个指针来比较。比较是否对称的树,就是比较除root外,左子树的左孩子和右子树的右孩子是否值相等,以及左子树的右孩子和右子树的左孩子是否值相等,然后从上往下,每个节点都这么比较。关键是:每次recursive需要两次比较和两次调用自己。因此,函数结构:需要两个指针分别表示左右指针,同时两次比较调用自己。
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.
s思路:
1. 树,选用那种遍历方式最快?要比较是否左右镜像对称。初看,想到用deque的bfs,把每层的数先全部放入deque,然后每次从两端取指针,看是否指针要么都null,要么都有、且数字相等。用deque还是麻烦,又想到可以用两个queue就可以解决问题,对root的左子树所有节点用一个queue来装,右子树用另一个queue来装。左子树每一层从左往右装,右子树每一层从右往左装,分别读出来比较即可!!妙啊!
2. 这个用两个queue的方法,应该是思维上受到上一题比较两个树是否相等的方法启发!
2. 再思考,还可以recursive. 用两个指针来比较。比较是否对称的树,就是比较除root外,左子树的左孩子和右子树的右孩子是否值相等,以及左子树的右孩子和右子树的左孩子是否值相等,然后从上往下,每个节点都这么比较。关键是:每次recursive需要两次比较和两次调用自己。因此,函数结构:需要两个指针分别表示左右指针,同时两次比较调用自己。
//方法1:recursive class Solution { public: bool helper(TreeNode* l,TreeNode* r){ if(!l&&!r) return true; if(l&&r&&l->val==r->val) return helper(l->left,r->right)&&helper(l->right,r->left); return false;//bug:两个指针,讨论空还是有,四种组合都要面面俱到 } bool isSymmetric(TreeNode* root) { if(!root) return true; return helper(root->left,root->right); } }; //方法2:iterative class Solution { public: bool isSymmetric(TreeNode* root) { if(!root) return true; queue<TreeNode*> pp,qq; pp.push(root->left); qq.push(root->right); while(!pp.empty()||!qq.empty()){ int psize=pp.size(),qsize=qq.size(); //if(psize!=qsize) return false; TreeNode* cur_p;//笔误bug:每次容易把TreeNode顺手写成ListNode TreeNode* cur_q; for(int i=0;i<psize;i++){ cur_p=pp.front(); cur_q=qq.front(); pp.pop();qq.pop(); if(!cur_p&&cur_q||cur_p&&!cur_q||cur_p&&cur_q&&cur_p->val!=cur_q->val) return false; if(cur_p&&cur_q&&cur_p->val==cur_q->val){ pp.push(cur_p->left); pp.push(cur_p->right); qq.push(cur_q->right); qq.push(cur_q->left); } } } return true; } };
相关文章推荐
- Leetcode-标签为Tree 101. Symmetric Tree
- Leetcode 101. Symmetric Tree
- leetcode 101. Symmetric Tree
- LeetCode 101. Symmetric Tree
- leetcode101. Symmetric Tree
- leetcode 每日一题 101. Symmetric Tree
- 小白笔记---------------------------------leetcode(101. Symmetric Tree )
- 【LeetCode】101. Symmetric Tree-对称树/镜像树
- [Leetcode] 101. Symmetric Tree
- LeetCode-101. Symmetric Tree
- 【一天一道LeetCode】#101. Symmetric Tree
- LeetCode 101. Symmetric Tree
- 第三周:[LeetCode]101. Symmetric Tree
- LeetCode-101. Symmetric Tree-using C
- 【LeetCode】 101. Symmetric Tree
- 【LeetCode】101. Symmetric Tree
- [LeetCode] 101. Symmetric Tree
- Leetcode 101. Symmetric Tree
- 【leetcode】101. Symmetric Tree【java】递归和非递归两种方法
- 【一天一道LeetCode】#101. Symmetric Tree