Symmetric Tree

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.

不管是递归还是非递归,找到关系就好做。

所谓对称,也就是:

1、left对应right

2、left->left对应right->right

3、left->right对应right->left

解法一:递归

/**
* 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) {
if(!root)
return true; return isSymTree(root->left, root->right);
}
bool isSymTree(TreeNode* p, TreeNode* q)
{
if(!isSameNode(p, q))
return false;
if(!p && !q)
return true;
return isSymTree(p->left, q->right) && isSymTree(p->right, q->left);
}
bool isSameNode(TreeNode* p, TreeNode* q)
{
if(!p && !q)
return true;
if((!p && q) || (p && !q) || (p->val != q->val))
return false;
return true;
}
};

解法二:非递归

使用两个队列,对左右子树分别进行层次遍历。

进队时的对应元素比较即可。

/**
* 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) {
if(!root)
return true; if(!isSameNode(root->left, root->right))
return false;
if(!root->left && !root->right)
return true; queue<TreeNode*> lqueue;
queue<TreeNode*> rqueue;
lqueue.push(root->left);
rqueue.push(root->right);
while(!lqueue.empty() && !rqueue.empty())
{
TreeNode* lfront = lqueue.front();
TreeNode* rfront = rqueue.front();
lqueue.pop();
rqueue.pop(); if(!isSameNode(lfront->left, rfront->right))
return false;
if(lfront->left && rfront->right)
{
lqueue.push(lfront->left);
rqueue.push(rfront->right);
} if(!isSameNode(lfront->right, rfront->left))
return false;
if(lfront->right && rfront->left)
{
lqueue.push(lfront->right);
rqueue.push(rfront->left);
}
}
return true;
}
bool isSameNode(TreeNode* p, TreeNode* q)
{
if(!p && !q)
return true;
if((!p && q) || (p && !q) || (p->val != q->val))
return false;
return true;
}
};

【LeetCode】101. Symmetric Tree (2 solutions)的更多相关文章

  1. 【LeetCode】101. Symmetric Tree 对称二叉树(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 [LeetCode] 题目地址 ...

  2. 【LeetCode】101 - Symmetric Tree

    Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...

  3. 【一天一道LeetCode】#101. Symmetric Tree

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  4. 【easy】101. Symmetric Tree

    判断一棵二叉树是否对称 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left ...

  5. 【LeetCode】100. Same Tree (2 solutions)

    Same Tree Given two binary trees, write a function to check if they are equal or not. Two binary tre ...

  6. 【LeetCode】199. Binary Tree Right Side View 解题报告(Python)

    [LeetCode]199. Binary Tree Right Side View 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/probl ...

  7. Leetcode之101. Symmetric Tree Easy

    Leetcode 101. Symmetric Tree Easy Given a binary tree, check whether it is a mirror of itself (ie, s ...

  8. 【LeetCode】145. Binary Tree Postorder Traversal

    Difficulty: Hard  More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/binary-tree-pos ...

  9. Leetcode 笔记 101 - Symmetric Tree

    题目链接:Symmetric Tree | LeetCode OJ Given a binary tree, check whether it is a mirror of itself (ie, s ...

随机推荐

  1. MYSQL学习笔记 (二)对数据库结构的增删改查

    显示数据库 show databases; 选择数据库 use database;//其实database为数据库的名字 创建表 create table tbclass( id int not nu ...

  2. Java中文件与字节数组转换

    注:来源于JavaEye 文件转化为字节数组: http://www.javaeye.com/topic/304980 /** * 文件转化为字节数组 * * @param file * @retur ...

  3. Spring JavaConfig @Import实例

    一般来说, 需要按模块或类别 分割Spring XML bean文件 成多个小文件, 使事情更容易维护和模块化. 例如, <beans xmlns="http://www.spring ...

  4. 软件包管理 rpm yum apt-get dpkg

    http://blog.csdn.net/ljq1203/article/details/7401616

  5. [Asp.net]AspNetPager分页组件

    引言 在基于Asp.net的内网系统中,分页功能是最常用的,用的最多的组件就是AspNetPager. AspNetPager 官网:http://www.webdiyer.com/aspnetpag ...

  6. 前端 fetch 通信

    随着前端异步的发展, XHR 这种耦合方式的书写不利于前端异步的 Promise 回调. 而且,写起来也是很复杂. fetch API 本来是在 SW(ServiceWorkers) 中提出的, 不过 ...

  7. 微信开发之如何使用开发工具--weixin-java-tools

    一.前沿 微信公众平台由于没有提供针对语言的开发包,只公布了一个基于Http协议的接口和加解密的算法sdk,这样给微信公众号的开发者带来很多工作量,除了实现业务逻辑外,还需要自己处理底层的接口协议细节 ...

  8. cocos2d-x 环境搭建

        刚搬到博客园,第一次在这写博.有点小激动啊~~     闲话不多说,这次想做一个专题,针对最近比较流行的手游开发引擎cocos2d-x,希望大家不吝赐教~     本节主要针对环境搭建方面进行 ...

  9. DCI:The DCI Architecture: A New Vision of Object-Oriented Programming

    SummaryObject-oriented programming was supposed to unify the perspectives of the programmer and the ...

  10. OAuth:第一天学习OAuth

    收集的一些资料 http://baike.baidu.com/view/3948029.htm. http://oauth.net/. 使用百度的OAuth服务进行测试 代码下载:http://yun ...