当前位置:网站首页>二叉树 | 翻转二叉树 | 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
边栏推荐
- FPGA - Memory Resources of 7 Series FPGA Internal Structure -03- Built-in Error Correction Function
- 分享一个后台管理系统可拖拽式组件的设计思路
- 链表相加(二)
- Redis
- Power system power flow calculation (Newton-Raphson method, Gauss-Seidel method, fast decoupling method) (Matlab code implementation)
- y93.第六章 微服务、服务网格及Envoy实战 -- Envoy配置(四)
- 2021IDEA创建web工程
- 美味的佳肴
- B站数据分析岗实习生面试记录
- 元宇宙社交应用,靠什么吸引用户「为爱发电」?
猜你喜欢
OneNote tutorial, how to organize notebooks in OneNote?
Why general company will say "go back messages such as" after the end of the interview, rather than just tell the interviewer the result?
Nodes in the linked list are flipped in groups of k
LeetCode每日两题02:反转字符串中的单词 (均1200道)
BM7 list entry in central
测试4年感觉和1、2年时没什么不同?这和应届生有什么区别?
LeetCode Daily 2 Questions 02: Reverse the words in a string (1200 each)
基于交流潮流的电力系统多元件N-k故障模型研究(Matlab代码实现)【电力系统故障】
【640. Solving Equations】
mmpose关键点(一):评价指标(PCK,OKS,mAP)
随机推荐
MySQL学习笔记(1)——基础操作
EL表达式
留言有奖|OpenBMB x 清华大学NLP:大模型公开课更新完结!
实例051:按位与
ASCII, Unicode and UTF-8
String类的常用方法
PyQt5 窗口自适应大小
RK3399 platform development series explanation (kernel-driven peripherals) 6.35, IAM20680 gyroscope introduction
H3C S5130 IRF做堆叠
Black cats take you learn Makefile article 13: a Makefile collection compile problem
port forwarding
【640. 求解方程】
uni-app微信小程序——下拉多选框
12 Recurrent Neural Network RNN2 of Deep Learning
Addition of linked lists (2)
VulnHub之DC靶场下载与DC靶场全系列渗透实战详细过程
Service - DNS forward and reverse domain name resolution service
y93.第六章 微服务、服务网格及Envoy实战 -- Envoy配置(四)
68: Chapter 6: Develop article services: 1: Content sorting; article table introduction; creating [article] article services;
The Thread State,