101. 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
代码如下:
/**
* 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 ...
- <LeetCode OJ> 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 ...
随机推荐
- 利用K2和Microsoft Dynamics CRM构建业务App的5大理由
Microsoft Dynamics CRM提供了一个绝佳的客户关系管理平台,使您能够创建各种以客户为中心的解决方案.然而,通过将K2的企业业务流程功能与Microsoft Dynamics CRM相 ...
- 全国行政区划代码(json对象版)
var area = {"110000":"北京市","110100":"北京市","110101" ...
- java jdbc----mysql的select、insert、update、delete
//-----------------------------------select---------------------------------- import java.sql.Connec ...
- OpenCV之响应鼠标(三):响应鼠标信息
转自:http://blog.csdn.net/haihong84/article/details/6599838 程序代碼如下: #include <cv.h>#include < ...
- IOS 导航栏属性设置
IOS 7 以上系统导航栏: [[UINavigationBar appearance] setTintColor:[UIColor whiteColor]]; // 返回按钮颜色 [UINaviga ...
- 理解Objective C 中id
什么是id,与void *的区别 id在Objective C中是一个类型,一个complier所认可的Objective C类型,跟void *是不一样的,比如一个 id userName, 和vo ...
- Objective-C中NSValue的使用
我们在C/C++开发中常会用到结构体来帮助我们简单封装基本数据类型,在Objective-C中我们也可以使用结构体来完成数据类型的封装.同时,Cocoa Touch还提供了一个NSValue来帮助我们 ...
- mycat启动后,用Navicat Premium 连接报 "2013"
最近在学习mycat,启动后,用Navicat Premium 连接报 "2013" Lost Connection During Query ,经过一顿百度也没发现是怎么回事, ...
- (转)xcode5.0.2下国际化图文解说
原文:http://blog.csdn.net/dragoncheng/article/details/6703311 xcode5.0.2下国际化图文解说 分类: ...
- 《view programming guide for iOS 》之可以使用动画效果的属性
frame—Use this to animate position and size changes for the view. ,框架,可以视图动态改变大小和位置 bounds—Use this ...