[LeetCode&Python] Problem 101. Symmetric Tree
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
For example, this binary tree [1,2,2,3,4,4,3]
is symmetric:
1
/ \
2 2
/ \ / \
3 4 4 3
But the following [1,2,2,null,3,null,3]
is not:
1
/ \
2 2
\ \
3 3
BFS and Iterative:
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution(object):
def isSymmetric(self, root):
"""
:type root: TreeNode
:rtype: bool
"""
que=[root]
while que:
check=[]
n=len(que)
for i in range(n):
node=que.pop(0)
if node:
que.append(node.left)
que.append(node.right)
check.append(node.val)
else:
check.append(None)
n=len(check)
for i in range(n):
if check[i]!=check[n-i-1]:
return False
return True
Recursion:
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution(object):
def isSymmetric(self, root):
"""
:type root: TreeNode
:rtype: bool
"""
def findsys(node1,node2):
if node1==None and node2==None:
return True
if node1==None or node2==None:
return False
return node1.val==node2.val and findsys(node1.left,node2.right) and findsys(node1.right,node2.left) return findsys(root,root)
[LeetCode&Python] Problem 101. Symmetric Tree的更多相关文章
- 【leetcode❤python】101. Symmetric Tree
#-*- coding: UTF-8 -*-# Definition for a binary tree node.# class TreeNode(object):# def __init_ ...
- <LeetCode OJ> 101. Symmetric Tree
101. Symmetric Tree My Submissions Question Total Accepted: 90196 Total Submissions: 273390 Difficul ...
- [LeetCode&Python] Problem 257. Binary Tree Paths
Given a binary tree, return all root-to-leaf paths. Note: A leaf is a node with no children. Example ...
- [LeetCode&Python] Problem 107. Binary Tree Level Order Traversal II
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...
- [LeetCode&Python] Problem 563. Binary Tree Tilt
Given a binary tree, return the tilt of the whole tree. The tilt of a tree node is defined as the ab ...
- [LeetCode&Python] Problem 100. Same Tree
Given two binary trees, write a function to check if they are the same or not. Two binary trees are ...
- [LeetCode&Python] Problem 429. N-ary Tree Level Order Traversal
Given an n-ary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- [LeetCode&Python] Problem 589. N-ary Tree Preorder Traversal
Given an n-ary tree, return the preorder traversal of its nodes' values. For example, given a 3-ary ...
- [LeetCode&Python] Problem 590. N-ary Tree Postorder Traversal
Given an n-ary tree, return the postorder traversal of its nodes' values. For example, given a 3-ary ...
随机推荐
- EF Core 生成数据库
1.运行cmd,切换到打开项目所在文件夹,输入下面的命令 dotnet ef migrations add Initial 建立并初始化数据库 dotnet ef database update ...
- 【vue学习】vue 2.0版本以上创建项目的的步骤
一.环境准备 1.vue项目依赖 node.js npm,需要先安装node和npm,先检查本地是否安装node.npm 快捷键win+r 输入cmd 弹出操作框,如果电脑已经安装git,直接右 ...
- Ubuntu开机启动roscore服务的设置
1.在/etc/init.d中添加启停脚本ros_daemon.bash: #!/bin/bash ### BEGIN INIT INFO # Provides: ros_daemon.bash # ...
- 10ci
- js 取一定范围内的整数
function selectNum(lowNum,upNum) { var num = upNum-lowNum+1; // Math.floor() 向下取整 return Math.floor( ...
- Oracle数据库字段数据拆分成多行(REGEXP_SUBSTR函数)
做多选功能时为了简便,会在某个字段中存储多个值,保存时虽然省事,但后续的查询统计时还需要拆分数据才行,因此这时需要将字段内的值分成多行以便后续使用. 下面这个例子实现了字段内数据的拆分: --创建测试 ...
- 固定div的位置——不随窗口大小改变为改变位置
百度首页示例: 我给二维码,和下面文本固定位置 这时html代码 <div id="bar_code"> <div class="img_put&quo ...
- 20145338 《网络对抗》逆向及Bof基础实验
逆向及Bof基础实验 实践目标 ·本次实践的对象是一个名为pwn1的linux可执行文件. ·该程序正常执行流程是:main调用foo函数,foo函数会简单回显任何用户输入的字符串. ·该程序同时包含 ...
- tfs增加用户
1.windows上添加用户 2.tfs对应项目添加该用户 3.注意: 要设置服务器对应的本地安全策略 从网络上允许该用户访问
- 定义一个Map集合,key和value不规定类型,任意放入数据,用keySet()和 entrySet()两种方式遍历出Map集合的数据
package com.lanxi.demo1_1_1; import java.util.HashMap; import java.util.Iterator; import java.util.M ...