101. Symmetric Tree (Tree, Queue; DFS, WFS)
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
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.
思路:要等到左儿子和右儿子的结果都知道了,才能判断当前节点的对称性,所以是后序遍历
法I:递归后序遍历
class Solution {
public:
bool isSymmetric(TreeNode *root) {
if(!root) return true; bool result;
if((root->left==NULL && root->right != NULL) ||(root->left!=NULL && root->right == NULL))
{
return false;
}
else if(root->left == NULL && root->right == NULL)
{
return true;
}
else
{
result = cmp(root->left, root->right);
}
}
bool cmp(TreeNode * node1, TreeNode* node2)
{
int result1 = true;
int result2 = true;
if(node1->val!=node2->val) return false;
else
{
//递归结束条件:至少有一个节点为NULL
if((node1->left==NULL && node2->right != NULL) ||
(node1->left!=NULL && node2->right == NULL)||
(node1->right!=NULL && node2->left == NULL)||
(node1->right==NULL && node2->left != NULL))
{
return false;
}
if((node1->left == NULL && node2->right == NULL)&&
(node1->right == NULL && node2->left== NULL))
{
return true;
} //互相比较的两个点,要比较节点1的左儿子和节点2的右儿子,以及节点1的右儿子和节点2的左儿子
if(node1->left != NULL)
{
result1 = cmp(node1->left,node2->right);
}
if(node1->right != NULL)
{
result2 = cmp(node1->right,node2->left);
}
return (result1 && result2);
}
}
};
法II:用队列实现层次遍历(层次遍历总是用队列来实现)
class Solution {
public:
bool isSymmetric(TreeNode *root) {
if(root == NULL) return true;
queue<TreeNode*> q;
q.push(root->left);
q.push(root->right);
TreeNode *t1, *t2;
while(!q.empty()){
t1 = q.front();
q.pop();
t2 = q.front();
q.pop();
if(t1 == NULL && t2 == NULL)
continue;
if(t1 == NULL || t2 == NULL || t1->val != t2->val)
return false;
q.push(t1->left);
q.push(t2->right);
q.push(t1->right);
q.push(t2->left);
}
return true;
}
};
101. Symmetric Tree (Tree, Queue; DFS, WFS)的更多相关文章
- 101. Symmetric对称 Tree
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
- [leetcode] 101. Symmetric Tree 对称树
题目大意 #!/usr/bin/env python # coding=utf-8 # Date: 2018-08-30 """ https://leetcode.com ...
- <LeetCode OJ> 101. Symmetric Tree
101. Symmetric Tree My Submissions Question Total Accepted: 90196 Total Submissions: 273390 Difficul ...
- Leetcode之101. Symmetric Tree Easy
Leetcode 101. Symmetric Tree Easy Given a binary tree, check whether it is a mirror of itself (ie, s ...
- leetcode 100. Same Tree、101. Symmetric Tree
100. Same Tree class Solution { public: bool isSameTree(TreeNode* p, TreeNode* q) { if(p == NULL &am ...
- (二叉树 DFS 递归) leetcode 101. Symmetric Tree
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
- 【LeetCode】101. Symmetric Tree 对称二叉树(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 [LeetCode] 题目地址 ...
- LeetCode 101. Symmetric Tree 判断对称树 C++
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
- [leetcode]101. Symmetric Tree对称树
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
随机推荐
- 解析xml(当节点中有多个子节点)
概要:解析一个xml,当一个节点中又包含多个子节点如何解析,对比一个节点中不包括其他节点的情况. 一,xml样例 <cisReports batNo="查询批次号" unit ...
- Visio2010建立ER图并直接导出为SQL语句
Visio2010建立ER图并直接导出为SQL语句 2013年08月20日 ⁄ 综合 ⁄ 共 2581字 ⁄ 字号 小 中 大 ⁄ 评论关闭 建立数据库时我们需要考虑数据之间的关系,为了理清数据之间的 ...
- Jenkins配置slave遇到“无法启动该应用程序”的问题
飞测说:最近在负责持续集成相关的工作,我们用的是jenkins+svn+maven+sonar, 今天在用slave这块出现了一个问题,排查了好久才解决,踩过的坑,现在和大家一起看看,希望对大家有帮助 ...
- 关于EPoll的个人理解
1.epoll 是I/o多路复用的一种解决方案,对比select的优点有: a.支持打开最大的文件描述符(可高达百万) b.效率并不随着描述符的增多而线性下降.select每次是轮询,所以描述符越多效 ...
- JFreeChart的简单使用
实例1:简单的饼图 public class Test { public static void main(String[] args) { //建立默认的饼图 DefaultPieDataset d ...
- web本地存储(localStorage、sessionStorage)
web 本地存储 (localStorage.sessionStorage) 说明 对浏览器来说,使用 Web Storage 存储键值对比存储 Cookie 方式更直观,而且容量更大,它包含两种:l ...
- ballerina 学习二十二 弹性服务
主要包含断路器模式,负载均衡模式,故障转移,重试 Circuit Breaker 参考代码 import ballerina/http; import ballerina/log; import ba ...
- winform 勾选可以改变框控件
public partial class UCCheck : UserControl { [Browsable(true), Category("修改属性"), Descripti ...
- sysbench 参数
1)插入指定条数的数据 --events=N limit for total number of events [0] --time=N limit for total execution time ...
- mysql-13处理重复数据
1.防止表中出现重复数据 在mysql数据表中设置指定的字段为主键或唯一索引来保证数据的唯一行. -- 方法1:指定主键 create `table person_tbl`( `first_name` ...