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. 类方法load和initialize的区别

    1.+load方法当类或分类添加到object-c runtime时被调用,子类的+load方法会在它所有父类的+load方法之后执行,而分类的+load方法会在它的主类的+load方法之后执行.但不 ...

  2. Windows Sysinternals Suite

    Windows Sysinternals Suite 是一套由微软官方免费提供的系统工具集,其中包含了大量超级实的优秀绿色小软件,譬如 Desktops (虚拟桌面).Process Explorer ...

  3. OpenERP实施记录(14):收款处理

    本文是<OpenERP实施记录>系列文章的一部分. 1. 在前面的文章中,销售订单确认时自动生成了客户发票,可以在 会计 > 客户 > 客户发票 查询,状态为"草稿& ...

  4. LaTeX图片环境 Picture environment

    Picture environment If you need to include simple diagrams or figures in your document, the picture  ...

  5. iOS开源项目:AwesomeMenu

    https://github.com/levey/AwesomeMenu 模仿Path的menu,使用CoreAnimation实现. 1.首先说使用 AwesomeMenuItem *starMen ...

  6. poj 3264 Balanced Lineup 题解

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u Subm ...

  7. calibre,完成对各种格式的电子书籍的管理和格式转换及同步

    Calibre是免费的电子书制作.阅读软件,源代码开放,拥有跨平台的设计,支持多个基于不同系统的便携式移动设备,包括苹果iPhone.Amazon电子书等设备.它是一个完整的电子图书馆,包括图书馆管理 ...

  8. 利用FPGA实现PCI总线接口及Windows驱动实现

    利用FPGA实现PCI总线接口及Windows驱动实现 关于PCI总线协议,资料网上.书本都是.这里我们仅仅对重点对利用FPGA实现PCI总线接口问题进行简单分析.下图是PCI总线接口信号: 配置空间 ...

  9. 【云计算】使用docker搭建nfs实现容器间共享文件

    首先介绍下今天的两个主角:nfs和docker nfs 是什么 NFS(Network File System)即网络文件系统,是FreeBSD支持的文件系统中的一种,它允许网络中的计算机之间通过TC ...

  10. RUP(Rational Unified Process),统一软件开发过程

    RUP(Rational Unified Process),统一软件开发过程 https://baike.baidu.com/item/RUP/8924595?fr=aladdin RUP最重要的它有 ...