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
代码如下:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public boolean isSymmetric(TreeNode root) {
if(root==null)
return true;
TreeNode left=root.left;
TreeNode right=root.right; if(LeftTraverse(left).equals(RightTraverse(right)))
return true; return false;
}
public List<String> LeftTraverse(TreeNode root){
List<String> list=new ArrayList<>();
if(root==null)
return list; list.add(String.valueOf(root.val));
if(root.left!=null)
list.addAll(LeftTraverse(root.left));
else
list.add(" "); if(root.right!=null)
list.addAll(LeftTraverse(root.right));
else
list.add(" ");
return list;
}
public List<String> RightTraverse(TreeNode root){
List<String> list=new ArrayList<>();
if(root==null)
return list; list.add(String.valueOf(root.val));
if(root.right!=null)
list.addAll(RightTraverse(root.right));
else
list.add(" "); if(root.left!=null)
list.addAll(RightTraverse(root.left));
else
list.add(" "); return list;
}
}
101. Symmetric Tree的更多相关文章
-
[leetcode] 101. Symmetric Tree 对称树
题目大意 #!/usr/bin/env python # coding=utf-8 # Date: 2018-08-30 """ https://leetcode.com ...
-
&;lt;LeetCode OJ&;gt; 101. Symmetric Tree
101. Symmetric Tree My Submissions Question Total Accepted: 90196 Total Submissions: 273390 Difficul ...
-
Leetcode之101. Symmetric Tree Easy
Leetcode 101. Symmetric Tree Easy Given a binary tree, check whether it is a mirror of itself (ie, s ...
-
leetcode 100. Same Tree、101. Symmetric Tree
100. Same Tree class Solution { public: bool isSameTree(TreeNode* p, TreeNode* q) { if(p == NULL &am ...
-
Leetcode 笔记 101 - Symmetric Tree
题目链接:Symmetric Tree | LeetCode OJ Given a binary tree, check whether it is a mirror of itself (ie, s ...
-
【LeetCode】101. Symmetric Tree (2 solutions)
Symmetric Tree Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its ...
-
(Tree) 101. Symmetric Tree
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
-
[LeetCode]题解(python):101 Symmetric tree
题目来源 https://leetcode.com/problems/symmetric-tree/ Given a binary tree, check whether it is a mirror ...
-
leetcode 101 Symmetric Tree ----- java
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
随机推荐
-
【夯实Nginx基础】Nginx工作原理和优化、漏洞
本文地址 原文地址 本文提纲: 1. Nginx的模块与工作原理 2. Nginx的进程模型 3 . NginxFastCGI运行原理 3.1 什么是 FastCGI ...
-
多个DataSet数据合并
DataSet ds = myIAppSet.GetHomeHottestList(siteID, , time); ].Rows.Count > ) { ds.Merge(ds1); } Me ...
-
java 24 - 3 GUI之添加按钮
需求:把按钮添加到窗体,并对按钮添加一个点击事件. A:创建窗体对象 B:创建按钮对象 C:把按钮添加到窗体 D:窗体显示 注意:这里对按钮添加点击事件,同样使用监听器. 但是,这里的按钮是组件,所以 ...
-
C++ Caption
主题 1. 设置控件的标题文本 2. 获取控件的标题文本 Caption属性 取得一个窗体的标题(caption)文字,或者一个控件的内容 红色的部分就是 Caption 标题 Set ...
-
【转】android Camera 中添加一种场景模式
http://blog.csdn.net/fulinwsuafcie/article/details/8833652 首先,来了解一下什么是场景模式. 最简单的方法当然是google了,这里有一篇文章 ...
-
C#不用COM组件导出数据到Excel中
<?xml version='1.0'?><?mso-application progid='Excel.Sheet'?><Workbook xmlns='urn:sch ...
-
关于Ajax无刷新分页技术的一些研究 c#
关于Ajax无刷新分页技术的一些研究 c# 小弟新手,求大神有更好的解决方案,指教下~ 以前做项目,用过GridView的刷新分页,也用过EasyUI的封装好的分页技术,最近在老项目的基础上加新功能, ...
-
Supervisor: A Process Control System
Supervisor: 进程控制系统 概述:Supervisor是一个 Client/Server模式的系统,允许用户在类unix操作系统上监视和控制多个进程,或者可以说是多个程序. 它与launch ...
-
getHibernateTemplate() VS getSession()
如题所示,对于这个问题,官网文档已给出答案,详见: /** * Obtain a Hibernate Session, either from the current transaction or * ...
-
Windows Server 2012 R2 双网卡绑定
双网卡绑定主要有以下两点好处: 1.实现网络容错:主主模式和主被模式 2.带宽聚合 首先准备工作需要两台虚拟机,Server01是目标服务器,需要有两块网卡,并且清空两块网卡的现有配置,Server0 ...