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 大家好,我是拿输出博客来督促自己刷题的老三,这一节我们 ...
随机推荐
- python中类的创建和实例化
python中同样使用关键字class创建一个类,类名称第一个字母大写,可以带括号也可以不带括号: python中实例化类不需要使用关键字new(也没有这个关键字),类的实例化类似函数调用方式: # ...
- mysql date
date_format(`time`, '%Y-%m-%d %h:%i:%s' ) as time
- 关于css样式错乱
在浏览器中的console中执行以下代码会有惊喜哦: [].forEach.call($$("*"), function(a) { a.style.outline = " ...
- 关于js的对象创建方法(简单工厂模式,构造函数模式,原型模式,混合模式,动态模式)
// 1.工厂方式创建对象:面向对象中的封装函数(内置对象) 简单来说就是封装后的代码,简单的工厂模式是很好理解的,关于它的作用,就是利用面向对象的方法,把一些对象封装,使一些占用空间多的,重复的代码 ...
- springboot2.1.3.RELEASE+jsp笔记war部署tomcat
springboot+jsp <packaging>war</packaging> <parent> <groupId>org.springframew ...
- 会话的保持和form表单
会话的保持和form表单 cookie 设置cookie from django.shortcuts import render, HttpResponse, redirect, reverse de ...
- psql备份和恢复(ubuntu)
备份 sudo pg_dump -U username -f filename.sql dbname 恢复 psql -U username -f filename.sql dbname -- ...
- 使用maven-tomcat7-plugins时调试出现source not found解决
直接看下面的步骤: 步骤1: 步骤2: 步骤3: 步骤4:
- jsp案例--展示数据库中的数据
一.什么是jsp? JAVA SERVER PAGES java的动态网页,servlet用来获取数据处理业务,擅长处理与java代码有关的内容.jsp展示数据,擅长处理与html有关的内容. 二.如 ...
- Linux查看和修改文件时间
参考http://www.361way.com/chang-file-time/1632.html 一:查看时间 1:查看文件的具体时间信息 File: `probn' Size: Blocks: I ...