LeetCode 101 对称二叉树的几种思路(Python实现)
对称二叉树
给定一个二叉树,检查它是否是镜像对称的。
例如,二叉树 [1,2,2,3,4,4,3] 是对称的。
1
/ \
2 2
/ \ / \
3 4 4 3 但是下面这个[1,2,2,null,3,null,3]则不是镜像对称的:
1
/ \
2 2
\ \
3 3 思路1:使用层次遍历解决,如果每一层都是对称的 那么该二叉树为对称(正好先做的层次遍历,发现这里可以直接用同样思路做,把空节点用' '空格代替 以保证对称)
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution:
def isSymmetric(self, root: TreeNode) -> bool:
if root == None:
return True
layer = [root]
res = []
while len(layer):
this_res = []
next_l = []
for n in layer:
if n == ' ':
this_res.append(' ')
continue
this_res.append(n.val)
if n.left:
next_l.append(n.left)
else:
next_l.append(' ')
if n.right:
next_l.append(n.right)
else:
next_l.append(' ')
for i in range(len(this_res)//2):
if this_res[i] != this_res[len(this_res)-i-1]:
return False
layer = next_l return True
递归判断:
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution:
def judge(self, left, right):
if left == None and right == None:
return True
if left != None and right == None:
return False
if left == None and right != None:
return False
if left.val != right.val:
return False
return self.judge(left.right, right.left) and self.judge(left.left, right.right) def isSymmetric(self, root: TreeNode) -> bool:
if root == None:
return True
return self.judge(root.left, root.right)
迭代:
def isSymmetric(self, root):
if not root:
return True
nodeList = [root.left,root.right]
while nodeList:
symmetricLeft = nodeList.pop(0)
symmetricRight = nodeList.pop(0)
if not symmetricLeft and not symmetricRight:
continue
if not symmetricLeft or not symmetricRight:
return False
if symmetricLeft.val != symmetricRight.val:
return False
nodeList.append(symmetricLeft.left)
nodeList.append(symmetricRight.right)
nodeList.append(symmetricLeft.right)
nodeList.append(symmetricRight.left)
return True
LeetCode 101 对称二叉树的几种思路(Python实现)的更多相关文章
- Java实现 LeetCode 101 对称二叉树
101. 对称二叉树 给定一个二叉树,检查它是否是镜像对称的. 例如,二叉树 [1,2,2,3,4,4,3] 是对称的. 1 / \ 2 2 / \ / \ 3 4 4 3 但是下面这个 [1,2,2 ...
- LeetCode 101. 对称二叉树(Symmetric Tree)
题目描述 给定一个二叉树,检查它是否是镜像对称的. 例如,二叉树 [1,2,2,3,4,4,3] 是对称的. 1 / \ 2 2 / \ / \ 3 4 4 3 但是下面这个 [1,2,2,null, ...
- LeetCode 101.对称二叉树 - JavaScript
题目描述:给定一个二叉树,检查它是否是镜像对称的. 题目分析 下面这种二叉树就是镜像对称的,符合题目要求: 1 / \ 2 2 / \ / \ 3 4 4 3 解法 1:递归检查 根据题目" ...
- php实现求对称二叉树(先写思路,谋而后动)
php实现求对称二叉树(先写思路,谋而后动) 一.总结 1.先写思路,谋而后动 二.php实现求对称二叉树 题目描述: 请实现一个函数,用来判断一颗二叉树是不是对称的.注意,如果一个二叉树同此二叉树的 ...
- LeetCode【101. 对称二叉树】
对称二叉树,就是左节点的左节点等于右节点的右节点,左节点的右节点等于右节点的左节点. 很自然就想到迭代与递归,可以创建一个新的函数,就是另一个函数不断的判断,返回在主函数. class Solutio ...
- Leetcode题目101.对称二叉树(简单)
题目描述: 给定一个二叉树,检查它是否是镜像对称的. 例如,二叉树 [1,2,2,3,4,4,3] 是对称的. 1 / \ 2 2 / \ / \ 3 4 4 3 但是下面这个 [1,2,2,null ...
- 【LeetCode】101. 对称二叉树
题目 给定一个二叉树,检查它是否是镜像对称的. 例如,二叉树 [1,2,2,3,4,4,3] 是对称的. 1 / \ 2 2 / \ / \ 3 4 4 3 但是下面这个 [1,2,2,null,3, ...
- 领扣(LeetCode)对称二叉树 个人题解
给定一个二叉树,检查它是否是镜像对称的. 例如,二叉树 [1,2,2,3,4,4,3] 是对称的. 1 / \ 2 2 / \ / \ 3 4 4 3 但是下面这个 [1,2,2,null,3,nul ...
- 【Leetcode】对称二叉树
递归法 执行用时 :12 ms, 在所有 C++ 提交中击败了43.44%的用户 内存消耗 :14.6 MB, 在所有 C++ 提交中击败了95.56%的用户 /** * Definition for ...
随机推荐
- css中的display(显示)和visibility(可见性)
display定义和用法 display 属性规定元素应该生成的框的类型. 说明 这个属性用于定义建立布局时元素生成的显示框类型.对于 HTML 等文档类型,如果使用 display 不谨慎会很危险, ...
- 13.Roman to Integer (HashTable)
Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 t ...
- mybatis中的factory工厂与Sqlsession
1.SqlSession的使用范围 SqlSession中封装了对数据库的操作,如:查询.插入.更新.删除等.通过SqlSessionFactory创建SqlSession,而SqlSessionFa ...
- Golang之strings包
只列举了部分函数方法的使用: 太多了....... package main import ( "fmt" "strings" ) func main() { ...
- ToList和ToDataTable(其中也有反射的知识)
using System;using System.Collections.Generic;using System.Data;using System.Linq;using System.Refle ...
- C语言基础第一次作业
一,1)大学和高中最大的不同是没有人天天看着你,请问大学理想的师生关系是?有何感想? 看了邹欣老师博客中写到的教学基础——师生关系后陷入沉思,邹欣老师在她的博客中直接否认了传统认知的师生关系——蜡烛, ...
- mutex 实现 只允许一个进程
static class Program { [STAThread] static void Main() { bool createdNew=false; Mutex mutex = new Mut ...
- ffmpeg只编译h264
./configure --arch=arm --cross-prefix=arm-none-linux-gnueabi- --extra-ldflags=-static --target-os=li ...
- 书籍索引 #C++
卷 计算机 的文件夹 PATH 列表卷序列号为 00000200 0001:8890F:.│ 21天学通C++.pdf│ C++ Primer Plus 第6版 中文版.pdf│ C++ Templa ...
- Baidu Map开发示例
1.获取SHA1码 在Eclipse中点击“Windows”----->“Preferences” ----->“Android” ----->“Build”如下图: 打开“Win+ ...