当前位置:网站首页>二叉树 | 翻转二叉树 | leecode刷题笔记
二叉树 | 翻转二叉树 | leecode刷题笔记
2022-08-10 22:23:00 【Begonia_cat】
跟随carl代码随想录刷题
语言:python
226. 简单
翻转二叉树
题目:给你一棵二叉树的根节点 root ,翻转这棵二叉树,并返回其根节点。
输入:root = [4,2,7,1,3,6,9]
输出:[4,7,2,9,6,3,1]
题目分析
思路:交换每个节点的左右节点即可root.left, root.right = root.right, root.left
方法选择:
- 前序遍历
- 中序遍历【不能用,因为会把所有节点交换两次】
- 后序遍历
- 层序遍历
完整代码如下
方法1.1:前序遍历——递归法
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def invertTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
if root == None:
return None
# 交换节点
root.left, root.right = root.right, root.left # 中
self.invertTree(root.left) # 左
self.invertTree(root.right) # 右
return root
方法1.2:后序遍历——递归法
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def invertTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
if root == None:
return None
self.invertTree(root.left) # 左
self.invertTree(root.right) # 右
# 交换节点
root.left, root.right = root.right, root.left # 中
return root
方法2.1:前序遍历——迭代法
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def invertTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
if not root:
return None
st = []
st.append(root)
while st:
node = st.pop()
node.left, node.right = node.right, node.left # 中
if node.right:
st.append(node.right) # 左
if node.left:
st.append(node.left) # 右
return root
方法2.2:后序遍历——迭代法
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def invertTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
if not root:
return None
st = []
st.append(root)
while st:
node = st.pop()
if node.right:
st.append(node.right) # 左
if node.left:
st.append(node.left) # 右
node.left, node.right = node.right, node.left # 中
return root
方法3:层序遍历
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def invertTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
if not root:
return None
from collections import deque
que = deque([root])
while que:
size = len(que)
cur = que.popleft()
cur.left, cur.right = cur.right, cur.left
if cur.left:
que.append(cur.left)
if cur.right:
que.append(cur.right)
return root
边栏推荐
- 【640. Solving Equations】
- 商家招募电商主播要考虑哪些内容
- CIKM2022 | Sequence Recommendation Based on Bidirectional Transformers Contrastive Learning
- MySQL学习笔记(2)——简单操作
- 留言有奖|OpenBMB x 清华大学NLP:大模型公开课更新完结!
- Redis
- 亲测有效|处理风控数据特征缺失的一种方法
- 边缘与云计算:哪种解决方案更适合您的连接设备?
- BM13判断一个链表是否为回文结构
- gcc492 compile `.rodata‘ can not be used when making a PIE object; recompile with -fPIE
猜你喜欢
EL表达式
常用代码扩展点设计方式
What is Jmeter? What are the principle steps used by Jmeter?
RK3399平台开发系列讲解(内核驱动外设篇)6.35、IAM20680陀螺仪介绍
August 10, 2022: Building Web Applications for Beginners with ASP.NET Core -- Creating Web UIs with ASP.NET Core
12 Recurrent Neural Network RNN2 of Deep Learning
2021 IDEA creates web projects
罗克韦尔AB PLC RSLogix5000中计数器指令使用方法介绍
这款可视化工具神器,更直观易用!太爱了
分享一个后台管理系统可拖拽式组件的设计思路
随机推荐
Merge k sorted linked lists
CIKM2022 | 基于双向Transformers对比学习的序列推荐
Shell 编程--Sed
字节跳动原来这么容易就能进去...
边缘与云计算:哪种解决方案更适合您的连接设备?
美味的佳肴
MySQL: MySQL Cluster - Principle and Configuration of Master-Slave Replication
JS学习 2022080
常用代码扩展点设计方式
make & cmake
实例051:按位与
How many threads does LabVIEW allocate?
DC-7靶场下载及渗透实战详细过程(DC靶场系列)
Pro-test is effective | A method to deal with missing features of risk control data
阿里云贾朝辉:云XR平台支持彼真科技呈现国风科幻虚拟演唱会
MySQL:MySQL的集群——主从复制的原理和配置
【开源教程5】疯壳·开源编队无人机-飞控固件烧写
3598. Binary tree traversal (Huazhong University of Science and Technology exam questions)
水果沙拉酱
Black cats take you learn Makefile article 13: a Makefile collection compile problem