[Leetcode] 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.
confused what"{1,#,2,3}"means? >read more on how binary tree is serialized on OJ.
The serialization of a binary tree follows a level order traversal, where '#' signifies a path terminator where no node exists below.
Here's an example:
1
/ \
2 3
/
4
\
5
The above binary tree is serialized as"{1,2,3,#,#,4,#,#,5}".
/**
* 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==nullptr)
return true; queue<TreeNode *> Q1;
queue<TreeNode *> Q2; Q1.push(root->left);
Q2.push(root->right); while( !Q1.empty())
{
TreeNode *node1=Q1.front();
TreeNode *node2=Q2.front(); Q1.pop();
Q2.pop(); if(node1==nullptr&&node2==nullptr)
continue;
if(node1==nullptr||node2==nullptr)
return false;
if(node1->val !=node2->val)
return false; Q1.push(node1->left);
Q1.push(node1->right);
Q2.push(node2->right);
Q2.push(node2->left); }
return true;
}
}; /* //主体的另一种写法
while (!q1.empty() && !q2.empty())
{
TreeNode *node1 = q1.front();
TreeNode *node2 = q2.front();
q1.pop();
q2.pop();
if((node1 && !node2) || (!node1 && node2)) return false;
if (node1)
{
if (node1->val != node2->val) return false;
q1.push(node1->left);
q1.push(node1->right);
q2.push(node2->right);
q2.push(node2->left);
}
}
*/
方法二:使用栈。特别值得注意的是:入栈的顺序,先左左,后右右,交替入栈,这样,出栈时才能保证同时从一层的两头向中间遍历。循环中,注意的条件:左右同时为0时,这种情况重新遍历即可;有一者为0,则说明不对称,返回false;对应的值不相等,也是返回false;到叶节点以后也是重新遍历未访问的结点即可。
/**
* 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==NULL||(root->left==NULL&&root->right==NULL))
return true; stack<TreeNode *> stk;
stk.push(root->left);
stk.push(root->right); while( !stk.empty())
{
TreeNode *rNode=stk.top();
stk.pop();
TreeNode *lNode=stk.top();
stk.pop(); if(rNode==NULL&&lNode=NULL)
continue;
if(rNode==NULL||lNode==NULL)
return false;
if(rNode->val !=lNode->val)
return false; //判断是否到叶节点,若是返回遍历其他
if(lNode->left==NULL&&lNode->right==NULL
&&rNode->left==NULL&&rNode->right==NULL)
continue;
else
{ //注意顺序,先左左后右右,交替入栈
stk.push(lNode->left);
stk.push(rNode->right);
stk.push(lNode->right);
stk.push(rNode->left);
}
}
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==NULL)
return true;
return subTreeSym(root->left,root->right);
} bool subTreeSym(TreeNode *lNode,TreeNode *rNode)
{
if(lNode==NULL&&rNode==NULL)
return true;
if(lNode==NULL||rNode==NULL) //和下面的那个不能颠倒顺序
return false;
if(lNode->val !=rNode->val)
return false; return subTreeSym(lNode->left,rNode->right)&&subTreeSym(lNode->right,rNode->left);
} };
[Leetcode] Symmetric tree 对称二叉树的更多相关文章
- 【LeetCode】101. Symmetric Tree 对称二叉树(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 [LeetCode] 题目地址 ...
- 二叉树系列 - [LeetCode] Symmetric Tree 判断二叉树是否对称,递归和非递归实现
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
- 【LeetCode】Symmetric Tree(对称二叉树)
这道题是LeetCode里的第101道题.是我在学数据结构——二叉树的时候碰见的题. 题目如下: 给定一个二叉树,检查它是否是镜像对称的. 例如,二叉树 [1,2,2,3,4,4,3] 是对称的. 1 ...
- [leetcode] 101. Symmetric Tree 对称树
题目大意 #!/usr/bin/env python # coding=utf-8 # Date: 2018-08-30 """ https://leetcode.com ...
- LeetCode: Symmetric Tree 解题报告
Symmetric Tree Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its ...
- LeetCode【101. 对称二叉树】
对称二叉树,就是左节点的左节点等于右节点的右节点,左节点的右节点等于右节点的左节点. 很自然就想到迭代与递归,可以创建一个新的函数,就是另一个函数不断的判断,返回在主函数. class Solutio ...
- [LeetCode] 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对称树
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
- 第28题:leetcode101:Symmetric Tree对称的二叉树
给定一个二叉树,检查它是否是镜像对称的. 例如,二叉树 [1,2,2,3,4,4,3] 是对称的. 1 / \ 2 2 / \ / \ 3 4 4 3 但是下面这个 [1,2,2,null,3,nul ...
随机推荐
- IDEA项目启动报Unable to open debugger port (127.0.0.1:51554): java.net.SocketException "socket closed"
启动报错: Unable to open debugger port (127.0.0.1:51554): java.net.SocketException "socket closed&q ...
- QP之QF原理
1.QP简介: 量子平台(Quantum Platform, 简称QP)是一个用于实时嵌入式系统的软件框架,QP是轻量级的.开源的.基于层次式状态机的.事件驱动的平台. QP包括事件处理器(QEP). ...
- makefile = 与 := 的区别
“=” make会将整个makefile展开后,再决定变量的值.也就是说,变量的值将会是整个makefile中最后被指定的值.看例子: x = foo y = $(x) bar ...
- 学习RUNOOB.COM进度二
MongoDB 概念解析 SQL术语/概念 MongoDB术语/概念 解释/说明 database database 数据库 table collection 数据库表/集合 row document ...
- SpringMVC接收前端传值有哪些方式?
有很多种,比如: 1.通过@RequestParam注解接收请求参数: 2.通过Bean封装,接收多个请求参数 3.通过@ModelAttribute绑定接收前端表单数据 4.通过@PathVaria ...
- 【TRICK】[0,n)中所有大小为k的子集的方法
<< k) - ; <<n)) { int x = comb & -comb, y = comb + x; comb = (((comb & ~y)/x)> ...
- 网站的robots.txt文件
什么是robots.txt? robots.txt是一个纯文本文件,是爬虫抓取网站的时候要查看的第一个文件,一般位于网站的根目录下.robots.txt文件定义了爬虫在爬取该网站时存在的限制,哪些部分 ...
- express与ejs,ejs在Linux上面的路径问题
1.学习使用ejs模板(这个是ejs.js) var express = require('express'); var app = express(); app.set("view eng ...
- axios应用
Skip to content Features Business Explore Marketplace Pricing Sign in or Sign up Watch929 St ...
- SpringBoot学习:IDEA中快速搭建springboot项目
项目下载地址:http://download.csdn.net/detail/aqsunkai/9805821 (一)IDEA中创建maven web项目 创建好项目后设置项目的编译路径: (二)引入 ...