leetcode101
/**
* Definition for a binary tree node.
* public class TreeNode {
* public int val;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int x) { val = x; }
* }
*/
public class Solution {
Queue<TreeNode> Q = new Queue<TreeNode>(); private List<string> GetFloorString()
{
var floor = new List<string>(); var count = ;
while (Q.Count > )
{
var n = Q.Dequeue();
if (n.left != null)
{
floor.Add(n.left.val.ToString());
Q.Enqueue(n.left);
count++;
}
else
{
floor.Add("x");
}
if (n.right != null)
{
floor.Add(n.right.val.ToString());
Q.Enqueue(n.right);
count++;
}
else
{
floor.Add("x");
}
}
return floor;
} public bool IsSymmetric(TreeNode root)
{
if (root == null)
{
return true;
}
else
{
Q.Enqueue(root);
var str = GetFloorString(); int index = ;//起始索引
var xcount = ;
//len长度
for (int len = ; index + len <= str.Count && len > ; len = * (len - xcount))
{
xcount = ;
var l = new List<string>();//str.Substring(index, len);
for (int i = index; i < index + len; i++)
{
l.Add(str[i]);
} var halflen = len / ;
var l1 = new List<string>();
for (int i = ; i < halflen; i++)
{
l1.Add(l[i]);
}
//var s2 = s.Substring(halflen, halflen);
var l2 = new List<string>();
for (int i = halflen; i < halflen + halflen; i++)
{
l2.Add(l[i]);
} l2.Reverse(); var s1 = "";
var s2 = ""; for (int i = ; i < l1.Count; i++)
{
s1 += l1[i];
} for (int i = ; i < l2.Count; i++)
{
s2 += l2[i];
} if (s1 != s2)
{
return false;
} foreach (var c in l)
{
if (c == "x")
{
xcount++;
}
} index = index + len;
} return true;
}
}
}
https://leetcode.com/problems/symmetric-tree/#/description
补充一个python的实现:
class Solution:
def isSymmetric(self, root: 'TreeNode') -> 'bool':
if root == None:
return True
return self.isSymmetric2(root.left,root.right) def isSymmetric2(self,left,right):
if left == None and right == None:
return True
if left == None or right == None:
return False
if left.val != right.val:
return False
return self.isSymmetric2(left.left,right.right) and self.isSymmetric2(left.right,right.left)
leetcode101的更多相关文章
- [LeetCode101]Symmetric Tree
题目: Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). F ...
- LeetCode-101.对称二叉树
链接:https://leetcode-cn.com/problems/symmetric-tree/description/ 给定一个二叉树,检查它是否是它自己的镜像(即,围绕它的中心对称). 例如 ...
- [Swift]LeetCode101. 对称二叉树 | Symmetric Tree
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
- 【leetcode-101】 对称二叉树
101. 对称二叉树 (1过) 给定一个二叉树,检查它是否是镜像对称的. 例如,二叉树 [1,2,2,3,4,4,3] 是对称的. 1 / \ 2 2 / \ / \ 3 4 4 3 但是下面这个 [ ...
- 第28题:leetcode101:Symmetric Tree对称的二叉树
给定一个二叉树,检查它是否是镜像对称的. 例如,二叉树 [1,2,2,3,4,4,3] 是对称的. 1 / \ 2 2 / \ / \ 3 4 4 3 但是下面这个 [1,2,2,null,3,nul ...
- leetcode-101. 判断对称树 · Tree + 递归
题面 判断给定二叉树是否对称. Note : empty tree is valid. 算法 1. 根节点判空,若空,则返回true;(空树对称) 2. 根节点不空,递归判断左右子树.如果左右孩子都空 ...
- leetcode102 Binary Tree Level Order Traversal
""" Given a binary tree, return the level order traversal of its nodes' values. (ie, ...
- leetcode_二叉树篇_python
主要是深度遍历和层序遍历的递归和迭代写法. 另外注意:因为求深度可以从上到下去查 所以需要前序遍历(中左右),而高度只能从下到上去查,所以只能后序遍历(左右中). 所有题目首先考虑root否是空.有的 ...
- LeetCode通关:连刷三十九道二叉树,刷疯了!
分门别类刷算法,坚持,进步! 刷题路线参考:https://github.com/youngyangyang04/leetcode-master 大家好,我是拿输出博客来督促自己刷题的老三,这一节我们 ...
随机推荐
- css 长用点
功能 : font-size: 0; 去除图片之间空白空隙
- 自用IP查询网址 - 地址 - 归属地 - 地理位置 - 2017.5
下面速度较快排行 http://city.ip138.com/ip2city.asp http://1212.ip138.com/ic.asp http://www.taobao.com/help/g ...
- JavaScript中的DOM及相关操作
一.什么是DOM JavaScript由ECMAScript.DOM和BOM三部分组成,其中DOM代表描述网页内容的方法和接口,即文档对象模型(Document Object Model).在网页上, ...
- 如何在Linux下添加函数库
如何为Linux增加库一. 静态库在Linux下的静态库是以.a为后缀的文件.1. 建静态库h1.c 源文件#include<stdio.h>void hello1(){printf(“t ...
- 着色器语言 GLSL (opengl-shader-language)入门大全
基本类型: 类型 说明 void 空类型,即不返回任何值 bool 布尔类型 true,false int 带符号的整数 signed integer float 带符号的浮点数 floating s ...
- 百战程序员9- IO流
1.IO是什么意思? data source是什么意思? IO:输入输出 data source:数据源 2.字节流和字符流有什么区别?输入流和输出流有什么区别? 分类 3.节点流和处理流有什么区别? ...
- 洛谷1101:单词方阵(DFS)
题目描述 给一n×nn \times nn×n的字母方阵,内可能蕴含多个"yizhong"单词.单词在方阵中是沿着同一方向连续摆放的.摆放可沿着888个方向的任一方向,同一单词摆放 ...
- Arch Linux 的休眠设置
https://wiki.archlinux.org/index.php/Power_management/Suspend_and_hibernate_(简体中文)https://wiki.archl ...
- 用python 实现一个栈
前言 Python本身已有顺序表(List.Tupple)的实现,所以这里从栈开始. 什么是栈 想象一摞被堆起来的书,这就是栈.这堆书的特点是,最后被堆进去的书,永远在最上面.从这堆书里面取一本书出来 ...
- golang cache--go-cache
go-cache是一款类似于memached 的key/value 缓存软件.它比较适用于单机执行的应用程序. go-cache实质上就是拥有过期时间并且线程安全的map,可以被多个goroutine ...