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 大家好,我是拿输出博客来督促自己刷题的老三,这一节我们 ...
 
随机推荐
- 拉格朗日乘子法&KKT条件
			
朗日乘子法(Lagrange Multiplier)和KKT(Karush-Kuhn-Tucker)条件是求解约束优化问题的重要方法,在有等式约束时使用拉格朗日乘子法,在有不等约束时使用KKT条件.前 ...
 - locate语法
			
1.命令格式:locate [参数] [文件] 2.命令功能:locate命令可以在搜寻数据库时快速找到档案,数据库由updatedb程序来更新,updatedb是由cron daemon周期性建立的 ...
 - Spring中的@Bean注解、@Configuration注解、@Value
			
1.首先是注册bean类:@Component.@Responsitory.@Controller.@Service.Configuration这些注解是把要实例化的对象装化成一个bean,放到Ioc ...
 - Python学习之路基础篇--01Python的基本常识
			
1 计算机基础 首先认识什么是CPU(Central Processing Unit),即中央处理器,相当于人类的大脑.内存,临时储存数据,断电即消失.硬盘,可以长久的储存数据,有固态硬盘,机械硬盘之 ...
 - linux下(Window当然也可以)解决idea创建maven项目导入过慢问题
			
1.正常创建maven web项目 2.见下图,选择加号 3.弹出的框中填入archetypeCatalog----internal,确定即可
 - 关于c风格的字符串的基本函数
			
1.两个字符串的比较 //如果两个字符串相等 strcmp(s1,s2)=; //如果s1>s2 strcmp(s1,s2)>; //如果s1<s2 strcmp(s1,s2)< ...
 - VUE2中使用mint-ui,日期选择picker
			
首先页面引入需要使用的组件 import { DatetimePicker,Toast,Popup,Picker } from 'mint-ui'; methods部分 openPicker () { ...
 - 编译Linux-4.15.1内核时遇到:“error : openssl/bio.h :No such file or folder”
			
如题: scripts/extract-cert.c::: fatal error: openssl/bio.h: No such file or directory compilation term ...
 - C# 比较两个路径是否指向同一对象
			
string path1 = @"c:\test\rootpath"; string path2 = @"C:\TEST\..\TEST\ROOTPATH"; ...
 - Vue组件的介绍与使用
			
组件系统是将一个大型的界面切分成一个一个更小的可控单元. 组件是可复用的,可维护的. 组件具有强大的封装性,易于使用. 大型应用中,组件与组件之间交互是可以解耦操作的. 全局组件的使用 <!DO ...