[LeetCode 题解]: Symmetric Tree
前言
【LeetCode 题解】系列传送门: http://www.cnblogs.com/double-win/category/573499.html
1.题目描述
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.
OJ's Binary Tree Serialization:
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}".
2. 题意
给定一颗二叉树,判断该树是否为左右对称的二叉树。
3. 思路
4: 解法
class Solution {
public:
bool isSymmetric(TreeNode *root){
return root? Symmetric(root->left,root->right):true;
}
bool Symmetric(TreeNode *left, TreeNode *right){
if(left==NULL && right==NULL) return true;// 左右孩子为空
if(!left || !right) return false; // 仅含有左子树或者右子树
return left->val == right->val
&& Symmetric(left->left,right->right)
&& Symmetric(left->right,right->left);
}
};![]() |
作者:Double_Win 出处: http://www.cnblogs.com/double-win/p/3891215.html 声明: 由于本人水平有限,文章在表述和代码方面如有不妥之处,欢迎批评指正~ |
[LeetCode 题解]: Symmetric Tree的更多相关文章
- 【LeetCode】Symmetric Tree 推断一棵树是否是镜像的
题目:Symmetric Tree <span style="font-size:18px;"><span style="font-size:18px; ...
- [LeetCode 题解]: Binary Tree Preorder Traversal
前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 1.题目描述 Given a bi ...
- [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 Symmetric Tree 二叉树
判断一棵树是否自对称 可以回忆我们做过的Leetcode 100 Same Tree 二叉树和Leetcode 226 Invert Binary Tree 二叉树 先可以将左子树进行Invert B ...
- (二叉树 DFS 递归) leetcode 101. Symmetric Tree
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
- [leetcode] 10. Symmetric Tree
这次我觉得我的智商太低,想了很久才写出来.题目是让求镜像二叉树判断,题目如下: Given a binary tree, check whether it is a mirror of itself ...
- LeetCode 101. 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 ----- java
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
随机推荐
- Java 简单UDP通信
Java手册 java.net Class DatagramSocket java.lang.Object java.net.DatagramSocket All Implemented Interf ...
- Mysql 授权远程访问
1.表示从任何主机连接到mysql服务器 GRANT ALL PRIVILEGES ON *.* TO 'user'@'%' IDENTIFIED BY 'password' WITH GRANT O ...
- [saiku] 简介、下载、安装和教程
一.简介 Saiku成立于2008年,由Tom Barber和Paul Stoellberger研发. 最初叫做Pentaho分析工具,起初是基于OLAP4J库用GWT包装的一个前端分析工具. 经过多 ...
- javaweb消息中间件——rabbitmq入门
概念:RabbitMQ是一款开源的消息中间件系统,由erlang开发,是AMQP的实现. 架构图大概如上. broker是消息队列的服务器,比如在linux上,我们安装的rabbitmq就是一个bro ...
- probably another instance of uWSGI is running on the same address (127.0.0.1:9090). bind(): Address already in use
probably another instance of uWSGI is running on the same address (127.0.0.1:9090). bind(): Address ...
- linux中用date命令获取昨天、明天或多天前后的日期
转自:http://www.linuxde.net/2011/10/1033.html 在实际操作中,一些脚本中会调用明天,或者昨天,或更多天前的日期,本文将叙述讲述用date命令实现时间的显示.在L ...
- cglib动态代理代码示例
cglib动态代理代码示例 引用包cglib-xxx.jar 非Maven项目还需要手动引用包asm-xxx.jar 业务类(不需要定义接口) cglib代理类(实现接口MethodIntercept ...
- mybatis sql语句等日志打印
加settings <?xml version="1.0" encoding="UTF-8"?><!DOCTYPE configuration ...
- UI / UX设计师如何玩转用户心理学原理?
以下内容由Mockplus团队翻译整理,仅供学习交流,Mockplus是更快更简单的原型设计工具. 众所周知,心理学在APP的用户体验设计中起着非常重要的作用.通过了解我们的设计是如何被感知的,我们可 ...
- c11时间库一个小例子
#pragma once #include <chrono> #include <string> #include <iostream> #include < ...
