[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 ...
随机推荐
- Apache Maven(五):插件
Maven的插件分如下两种: build plugins:该插件在项目构建阶段执行,它们都在<build>标签中设置. reporting plugins : 该插件在网站生成期间执行,他 ...
- ARM串口控制终端命令
配置开发板eth0网络: # ifconfig eth0 10.70.12.168
- PADS快捷键
问:在pads layout中怎样显示或隐藏铺铜的效果 答: 无模命令:po 或者spo 前者是平面层 后者是混合层. 同时你可以在ctrl+alt+c 色彩项中关闭 copper . 使用 无模命令 ...
- docker使用命令汇总
docker命令 docker ps 容器列表 docker ps -a 所有容器列表,包含未运行的容器 docker image ls 镜像列表 docker logs -f xxx 容器日志 do ...
- 第一个python代码
# -*- coding:utf-8 -*- user = raw_input("请输入用户名") passwd = raw_input("请输入密码") if ...
- Fibonacci使用递归和循环实现
#include<stdio.h> double Fibonacci(int i); double Fibonacci_(int i); int main(void) { int i; p ...
- vue---day04
1. Node.js 1.1 介绍: - Node.js 是一个JavaScript运行环境,实质上是对Chrome V8引擎的封装. - Node.js 不是一个 JavaScript 框架,不同于 ...
- Overview of the High Efficiency Video Coding (HEVC) Standard阅读笔记
1.INTRODUCTION High Efficiency Video Coding(HEVC) <-> H.265 MPEG-4 Advanced Video Coding(AVC) ...
- python, pycharm, virtualenv 的使用
创建虚拟环境,一次安装多个库 pip freeze > requirements.txt (库的名字都在里面) 产生requirements.txt文件 在另一个环境下使用 pip instal ...
- iOS中如何根据UIView获取所在的UIViewController
原理 Responder Chain 事件的响应者链 大概的传递规则就是从视图顶层的UIView向下到UIViewController再到RootViewController再到Window最后到Ap ...