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 大家好,我是拿输出博客来督促自己刷题的老三,这一节我们 ...
随机推荐
- Spring手动提交事务
// name的值根据spring配置文件的事物管理器的id而定 @Resource(name="transactionManager") private DataSourceTr ...
- Oracle 创建存储过程 提示权限不足或者提示表和视图不存在问题
grant create view to hospital; --授予查询权限 grant select any table to hospital; --授予权限 grant select any ...
- laravel 解决session保存不了,取不出的问题
555 上传服务器的时候 storage下的framework 没有上传啊
- Spring @Resource,@Autowired,@Qualifier的注解注入和区别
spring2.5提供了基于注解(Annotation-based)的配置,我们可以通过注解的方式来完成注入依赖.在Java代码中可以使用 @Resource或者@Autowired注解方式来经行注入 ...
- sql语言 含有包含关系的查询 (含mysql 和sql sever)
一.sql中查询包含关系的查询 sql语句中包含关系可以使用 in 和exist,但有些时候仅仅用这两个是不够的,还有表示方法是 not exist(b expect a )可以表示a包含b. 二. ...
- linux kernel driver debug
1. print printk(): never pr_debug(): always good dev_dbg(): prefered when you have a struct device o ...
- java 实现excel 导出功能
实现功能:java导出excel表 1.jsp代码 <form id="zhanwForm" action="<%=path%>/conferences ...
- Ajax(Asynchronous JavaScript )and xml
JavaScript的两种任务执行模式--同步(synchronous)和异步(Asynchronous) 同步模式 JavaScript的执行环境是单线程的,意味着一次只能执行一个任务,如果有多个任 ...
- 用变量替换指定的字符串,sed: -e 表达式 #1, 字符 29: “s”的未知选项
在shell脚本里,使用sed,然后用变量替换指定的字符串,一直出现这个错误:但是单独运行在外面可以 把分隔符/替换成#就可以: sed "s#revision=.*#revision=$s ...
- PHP使用文件锁解决高并发问题示例
新建一个.txt文件,文件中什么都不用写. [一].阻塞(等待)模式:(只要有其他进程已经加锁文件,当前进程会一直等其他进程解锁文件) <?php //连接数据库 $con=mysqli_con ...