LeetCode() Symmetric Tree
/**
* Definition for a binary tree node.
* 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;
queue<TreeNode*> q;
q.push(root->left);
q.push(root->right);
while(!q.empty()){
TreeNode* lt=q.front();
q.pop();
TreeNode* rt=q.front();
q.pop();
if(!rt&&!lt) continue;
if(!rt||!lt) return false;
if(lt->val!=rt->val) return false;
q.push(lt->left);
q.push(rt->right);
q.push(lt->right);
q.push(rt->left);
}
return true;
}
bool isSymmetricHelper(TreeNode* l,TreeNode* r){
if(!r&&!l) return true;
if((!r&&l)||(!l&&r)||(r->val!=l->val)) return false;
return isSymmetricHelper(l->left,r->right)&&isSymmetricHelper(l->right,r->left); }
};
LeetCode() Symmetric Tree的更多相关文章
- LeetCode: Symmetric Tree 解题报告
Symmetric Tree Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its ...
- [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 @ Python
原题地址:https://oj.leetcode.com/problems/symmetric-tree/ 题意:判断二叉树是否为对称的. Given a binary tree, check whe ...
- 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 判断二叉树是否对称,递归和非递归实现
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
- [Leetcode] Symmetric tree 对称二叉树
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
- Python3解leetcode Symmetric Tree
问题描述: Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). ...
- leetcode:Symmetric Tree【Python版】
#error caused by:#1:{} 没有考虑None输入#2:{1,2,2} 没有控制h和t#3:{4,-57,-57,#,67,67,#,#,-97,-97} 没有考虑负号,将s从str变 ...
- 【LeetCode】Symmetric Tree 推断一棵树是否是镜像的
题目:Symmetric Tree <span style="font-size:18px;"><span style="font-size:18px; ...
随机推荐
- solaris tar 命令exclude使用
压缩时需要排除指定目录,不知道什么原因在solaris中 tar cvf a.tar a --exclude=a/b/log --exclude = a/c/mm7log不生效, 最后使用了如下方法 ...
- 高精度快速预览打开dwg文件的CAD控件CAD Image DLL介绍及下载
CAD Image DLL对于DXF格式, DWG格式(AutoCAD R12 到AutoCAD 2004/2005), PLT 以及 HPGL/HPGL2文件都有快速的显示速度和精度,开发者再也不会 ...
- wpf学习笔记
1.菜单:普通菜单.上下文菜单(ContextMenu) <Menu HorizontalAlignment="Left" Height="20" Ver ...
- Java 基础知识相关好文章
1. 使用简单易懂的例子,分析了equals 和 hashCode 两个方法的异同,尤其中自定义类中对他们的重写,对Set等容器类的在插入时的判断是否相等的影响. http://blog.csdn.n ...
- VMware下利用ubuntu13.04建立嵌入式开发环境之二
之前在VMware中安装完Ubuntu系统,接下来开始设置开发中用到的服务和工具,以及系统设计. 1.安装VMware工具:打开VMware软件,在菜单->VM->Install VMwa ...
- U盘被写保护如何解除 (转)
U盘被写保护如何解除 在使用U盘的时候,一直都很正常,但是突然有一天,U盘被提示被写保护了,不能够进行读写数据,如果这时正着急使用,一定会被气疯了吧,其实解决这个问题,是非常简单的. U盘被写保护 ...
- 微软云创益大赛获奖团队风采:做一个中国特色的.Net源代码社区
为了强化云技术,落地云应用,彰显云价值,微软(中国)携手中国计算机报举办了“微软Cloud OS第二届云创益大赛”.本届大赛历时111天,共吸引了6647位个人组选手回答了70,078道题,59支参赛 ...
- node_modules\typescript\lib 未指向有效的 tsserver 安装 将禁用TypeScript 语言功能
Ionic2 项目中经常遇到这个问题 每次都找半天无果. 简单记录一下 粗暴的解决办法: 卸载ts并从新安装即可 //卸载typescript npm uninstall typescript // ...
- BaseDao代码,用于连接数据库实行增删改查等操作
在学习JavaWeb时会用到此代码,用于实行增删改查操作 1 package com.bdqn.dao; import java.sql.Connection; import java.sql.Dri ...
- MISC-极客大挑战-pen_and_apple.rar
额 一道MISC的题目 关键词:NTFS数据流隐藏 链接:http://pan.baidu.com/s/1c14PM3A 密码:d7hn 拿到的是一个rar压缩文件,解压得到一个无节操的最近很流行 ...