Problem Link:
https://oj.leetcode.com/problems/symmetric-tree/
To solve the problem, we can traverse the tree level by level. For each level, we construct an array of values of the length 2^depth, and check if this array is symmetric. The tree is symmetric only if all constructed arrays are all symmetric.
The code is as follows.
# Definition for a binary tree node
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution:
# @param root, a tree node
# @return a boolean
def isSymmetric(self, root):
"""
We traverse the tree from the root level by level.
For each level, we construct a integer array of length 2^depth,
then we check if the array is symmetric.
"""
if not root:
return True
q = [root]
next = True
while next:
next = False
new_q = []
numbers = []
for node in q:
if node:
numbers.append(node.val)
# Left child
if node.left:
next = True
new_q.append(node.left)
else:
new_q.append(None)
# Right child
if node.right:
next = True
new_q.append(node.right)
else:
new_q.append(None)
else:
numbers.append(0)
if not self.isSymmetricList(numbers):
return False
q = new_q
return True def isSymmetricList(self, lst):
low = 0
high = len(lst)-1
while low < high:
if lst[low] != lst[high]:
return False
low += 1
high -= 1
return True
【LeetCode OJ】Symmetric Tree的更多相关文章
-
【LEETCODE OJ】Binary Tree Postorder Traversal
Problem Link: http://oj.leetcode.com/problems/binary-tree-postorder-traversal/ The post-order-traver ...
-
【LeetCode OJ】Same Tree
Problem Link: https://oj.leetcode.com/problems/same-tree/ The following recursive version is accepte ...
-
【LeetCode OJ】Binary Tree Level Order Traversal
Problem Link: https://oj.leetcode.com/problems/binary-tree-level-order-traversal/ Traverse the tree ...
-
【LeetCode OJ】Binary Tree Zigzag Level Order Traversal
Problem Link: https://oj.leetcode.com/problems/binary-tree-zigzag-level-order-traversal/ Just BFS fr ...
-
【LeetCode OJ】Binary Tree Level Order Traversal II
Problem Link: https://oj.leetcode.com/problems/binary-tree-level-order-traversal-ii/ Use BFS from th ...
-
【LeetCode OJ】Binary Tree Maximum Path Sum
Problem Link: http://oj.leetcode.com/problems/binary-tree-maximum-path-sum/ For any path P in a bina ...
-
【LEETCODE OJ】Binary Tree Preorder Traversal
Problem Link: http://oj.leetcode.com/problems/binary-tree-preorder-traversal/ Even iterative solutio ...
-
【LeetCode OJ】Interleaving String
Problem Link: http://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 ...
-
【LeetCode OJ】Reverse Words in a String
Problem link: http://oj.leetcode.com/problems/reverse-words-in-a-string/ Given an input string, reve ...
随机推荐
-
SQL Server中的GUID
GUID(Global unique identifier)全局唯一标识符,它是由网卡上的标识数字(每个网卡都有唯一的标识号)以及 CPU 时钟的唯一数字生成的的一个 16 字节的二进制值. GUID ...
-
一次插入多条记录 [mysql]
调用多次INSERT语句不就可以插入多条记录了吗?但使用这种方法要增加服务器的负荷,因为,执行每一次SQL服务器都要同样对SQL进行分析.优化等操作.幸好MySQL提供了另一种解决方案,就是使用一条I ...
-
Jython安装步骤
1.下载安装包 2.执行安装 Java -jar [此处是下载的jython jar包名],或者双击jar包夜可以 3.配置环境变量 新增JYTHON_THOME的环境变量,并设置为安装路径. 配置c ...
-
[转]使用onclick跳转到其他页面/跳转到指定url
如果是本页显示可以直接用location,方法如下: ①onclick="javascript:window.location.href='URL'" ②onclick=" ...
-
csdn第四名
编号:1027时间:2016年7月18日11:10:42功能:csdn第四名URL :http://blog.csdn.net/yuanmeng001
-
从项目中总结的js知识点
1. 数字字符串和数字进行比较可以得出正确结果,却不能正确判断是否在一个数字数组中.如以下程序: var s = '8', n = 8, arr = [1,2,8,9]; console.log(s= ...
-
BZOJ 1018: [SHOI2008]堵塞的交通traffic(线段树)
题目:http://www.lydsy.com/JudgeOnline/problem.php?id=1018 用线段树维护区间连通性,对于每一个区间记录6个域表示(左上,左下)(左上,右上)(右上, ...
-
Zabbix 配置监控主机
1.新建主机: zabbix中的主机(Host)是要监控的网络实体(物理的,或者虚拟的);zabbix中,对于主机的定义非常灵活,它可以时一台物理服务器,一个网络交换机,一个虚拟机或者一些应用 zab ...
-
python全栈开发 * 23 面向对象 知识点汇总 * 180704
23 面向对象 -----特殊方法 1. isinstance(obj,类名) 判断对象是否是此类实例化或者此类的子类实例化出来的class A:passclass B(A):passb1=B()pr ...
-
datatable删除行之datatable.Rows[i].Delete()。标记之后行没有了
使用Delete()之后行消失了 先在for循环外加上dt.AcceptChanges(); 删除时在dt.AcceptChanges();