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. mybatis源码分析(8)-----事务(mybatis管理、spring管理)

    写在前面 接口:MyBatis的事务Transaction的接口有一下实现类 JdbcTransaction 由jdbc管理的事务(即利用Connection对象完成对事务的提交(commit()). ...

  2. InvalidateRect()与Invalidate()的用法(转)

    BOOL InvalidateRect(   HWND hWnd,           // 窗口句柄   CONST RECT* lpRect,   // 矩形区域   BOOL bErase    ...

  3. Level-shifting nixes need for dual power supply

    The AD736 true-rms-to-dcconverter is useful for many applications that require precise calculation o ...

  4. How to let TVirtualStringTree to display an icon in disabled state?

    How to let TVirtualStringTree to display an icon in disabled state? I need to display files in a dir ...

  5. 再见了,DM

        在DM奋斗了20个月之后,我终于有机会DM说再见.这我不是我第一次和DM说再见,因此我也不确定这次的再见是再也不见,还是再次见面.但有一点可以确定的是,在接下来相当长的一段时间内,我是没有机会 ...

  6. TurboLinux11system»adjtimex简介

    Adjtimex介绍 linux 系统有两个时钟:一个是由主板电池驱动的“Real Time Clock”也叫做RTC或者叫CMOS时钟,硬件时钟.当操作系统关机的时候,用这个来记录时间,但是对于运行 ...

  7. C# 弹出USB外接硬盘(U盘)

    最近一个项目需要通过代码来弹出USB外接硬盘设备,经过google找到了下面这个类库: http://www.codeproject.com/Articles/13530/Eject-USB-disk ...

  8. 小议使用“完整”的CSS的缺点

    1.浏览器支持的不一致性 浏览器的漏洞或缺乏支持的CSS功能,导致不同的浏览器显示出不同的CSS版面编排.例如在微软Internet Explorer6.0的旧版本 ,执行了许多自己的CSS2.0属性 ...

  9. php 上传视频的代码,

    <html> <head> <meta http-equiv="Content-Type" content="text/html; char ...

  10. C#控件一览表

    C#控件一览表 .窗体 .常用属性 ()Name属性:用来获取或设置窗体的名称,在应用程序中可通过Name属性来引用窗体. () WindowState属性: 用来获取或设置窗体的窗口状态. 取值有三 ...