101. Symmetric Tree

时间:2022-09-13 10:40:54

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的更多相关文章

  1. &lbrack;leetcode&rsqb; 101&period; Symmetric Tree 对称树

    题目大意 #!/usr/bin/env python # coding=utf-8 # Date: 2018-08-30 """ https://leetcode.com ...

  2. &amp&semi;lt&semi;LeetCode OJ&amp&semi;gt&semi; 101&period; Symmetric Tree

    101. Symmetric Tree My Submissions Question Total Accepted: 90196 Total Submissions: 273390 Difficul ...

  3. Leetcode之101&period; Symmetric Tree Easy

    Leetcode 101. Symmetric Tree Easy Given a binary tree, check whether it is a mirror of itself (ie, s ...

  4. leetcode 100&period; Same Tree、101&period; Symmetric Tree

    100. Same Tree class Solution { public: bool isSameTree(TreeNode* p, TreeNode* q) { if(p == NULL &am ...

  5. Leetcode 笔记 101 - Symmetric Tree

    题目链接:Symmetric Tree | LeetCode OJ Given a binary tree, check whether it is a mirror of itself (ie, s ...

  6. 【LeetCode】101&period; Symmetric Tree &lpar;2 solutions&rpar;

    Symmetric Tree Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its ...

  7. &lpar;Tree&rpar; 101&period; Symmetric Tree

    Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...

  8. &lbrack;LeetCode&rsqb;题解(python):101 Symmetric tree

    题目来源 https://leetcode.com/problems/symmetric-tree/ Given a binary tree, check whether it is a mirror ...

  9. leetcode 101 Symmetric Tree ----- java

    Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...

随机推荐

  1. 【夯实Nginx基础】Nginx工作原理和优化、漏洞

    本文地址 原文地址 本文提纲: 1.  Nginx的模块与工作原理    2.  Nginx的进程模型    3 . NginxFastCGI运行原理        3.1 什么是 FastCGI   ...

  2. 多个DataSet数据合并

    DataSet ds = myIAppSet.GetHomeHottestList(siteID, , time); ].Rows.Count > ) { ds.Merge(ds1); } Me ...

  3. java 24 - 3 GUI之添加按钮

    需求:把按钮添加到窗体,并对按钮添加一个点击事件. A:创建窗体对象 B:创建按钮对象 C:把按钮添加到窗体 D:窗体显示 注意:这里对按钮添加点击事件,同样使用监听器. 但是,这里的按钮是组件,所以 ...

  4. C&plus;&plus; Caption

    主题 1. 设置控件的标题文本 2. 获取控件的标题文本     Caption属性 取得一个窗体的标题(caption)文字,或者一个控件的内容   红色的部分就是 Caption 标题   Set ...

  5. 【转】android Camera 中添加一种场景模式

    http://blog.csdn.net/fulinwsuafcie/article/details/8833652 首先,来了解一下什么是场景模式. 最简单的方法当然是google了,这里有一篇文章 ...

  6. C&num;不用COM组件导出数据到Excel中

    <?xml version='1.0'?><?mso-application progid='Excel.Sheet'?><Workbook xmlns='urn:sch ...

  7. 关于Ajax无刷新分页技术的一些研究 c&num;

    关于Ajax无刷新分页技术的一些研究 c# 小弟新手,求大神有更好的解决方案,指教下~ 以前做项目,用过GridView的刷新分页,也用过EasyUI的封装好的分页技术,最近在老项目的基础上加新功能, ...

  8. Supervisor&colon; A Process Control System

    Supervisor: 进程控制系统 概述:Supervisor是一个 Client/Server模式的系统,允许用户在类unix操作系统上监视和控制多个进程,或者可以说是多个程序. 它与launch ...

  9. getHibernateTemplate&lpar;&rpar; VS getSession&lpar;&rpar;

    如题所示,对于这个问题,官网文档已给出答案,详见: /** * Obtain a Hibernate Session, either from the current transaction or * ...

  10. Windows Server 2012 R2 双网卡绑定

    双网卡绑定主要有以下两点好处: 1.实现网络容错:主主模式和主被模式 2.带宽聚合 首先准备工作需要两台虚拟机,Server01是目标服务器,需要有两块网卡,并且清空两块网卡的现有配置,Server0 ...