当前位置:网站首页>二叉树 | 翻转二叉树 | 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
边栏推荐
- BM7 list entry in central
- 带着昇腾去旅行:一日看尽金陵城里的AI胜景
- LeetCode每日两题02:反转字符串中的单词 (均1200道)
- Black cats take you learn Makefile article 13: a Makefile collection compile problem
- Detailed installation steps and environment configuration of geemap
- 高通平台开发系列讲解(应用篇)QCMAP应用框架介绍
- 企业云存储日常运行维护实践经验分享
- Translating scientific and technological papers, how to translate from Russian to Chinese
- BM13 determines whether a linked list is a palindrome
- shell脚本
猜你喜欢

Translating scientific and technological papers, how to translate from Russian to Chinese

边缘与云计算:哪种解决方案更适合您的连接设备?

基于交流潮流的电力系统多元件N-k故障模型研究(Matlab代码实现)【电力系统故障】

Introduction to the use of counter instructions in Rockwell AB PLC RSLogix5000

交换机和生成树知识点

web项目访问引用jar内部的静态资源

高通平台开发系列讲解(应用篇)QCMAP应用框架介绍

file IO-buffer

高学历毕业生,该学单片机还是plc?

MySQL学习笔记(1)——基础操作
随机推荐
QT笔记——用VS + qt 生成dll 和 调用生成的dll
Pro-test is effective | A method to deal with missing features of risk control data
BM7 list entry in central
如何成为一名正义黑客?你应该学习什么?
12 Recurrent Neural Network RNN2 of Deep Learning
配电网络扩展规划:考虑使用概率性能源生产和消费概况的决策(Matlab代码实现)
华为HCIE云计算之Fusion Access桌面云
艺术与科技的狂欢,阿那亚2022砂之盒沉浸艺术季
留言有奖|OpenBMB x 清华大学NLP:大模型公开课更新完结!
瑞幸咖啡第二季营收33亿:门店达7195家 更换CFO
电力系统潮流计算(牛顿-拉夫逊法、高斯-赛德尔法、快速解耦法)(Matlab代码实现)
GMT,UTC,CST,DST,RTC,NTP,SNTP,NITZ: 嵌入式的时间
3598. Binary tree traversal (Huazhong University of Science and Technology exam questions)
实例051:按位与
SDP
Splunk中解决数据质量问题常见日期格式化
Glide监听Activity生命周期源码分析
String类的常用方法
LeetCode Daily 2 Questions 02: Reverse the words in a string (1200 each)
gcc492 compile `.rodata‘ can not be used when making a PIE object; recompile with -fPIE
