当前位置:网站首页>【LeetCode】114. Expand binary tree into linked list
【LeetCode】114. Expand binary tree into linked list
2022-06-10 01:42:00 【LawsonAbs】
1. subject
2. thought
idea : Want to transform into a left chain subtree , And then again reverse Get the right chain subtree .
The implementation of the first step is a little difficult , But it can also be tried , The second step reverse It's simpler .
3. Code
# 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 flatten(self, root: TreeNode) -> None:
""" Do not return anything, modify root in-place instead. """
# Change to left chain form
self.dfs(root)
self.reverse(root)
# Flip binary tree
def reverse(self,root):
if root is None:
return None
left_root = self.reverse(root.left)
right_root = self.reverse(root.right)
root.left = right_root
root.right = left_root
return root
def dfs(self,root):
if root is None:
return
self.dfs(root.left)
tmp = root
while(tmp.left):
tmp = tmp.left
# The above one just finds the appropriate root node , But the right subtree has not been ( Sinicization )
self.dfs(root.right)
tmp.left = root.right
root.right = None # Set to empty
return
边栏推荐
- 【LeetCode】437. Path sum III
- Practice of Flink CDC + Hudi massive data entering the lake in SF
- 【LeetCode】128. 最长连续序列
- Nodejs报内部错误 TypeError: Cannot read property ‘destroy‘ of undefined的解决方法
- Node red series (II and III): develop a Gaode map panel dashboard in node red
- 自定义操作日志记录注解
- [从零开始学习FPGA编程-16]:快速入门篇 - 操作步骤2-4- Verilog HDL语言描述语言基本语法(软件程序员和硬件工程师都能看懂)
- [email protected]和[email protected]装载5-Fu(5-氟尿
- Industrial practice example of industrial defect detection based on openvino deployment
- Leetcode 700: search in binary search tree
猜你喜欢

Introduction to cross platform multimedia rendering engine OPR

TRichView and ScaleRichView 设置默认中文

Picture batch download + picture mosaic: multiple pictures constitute the Dragon Boat Festival Ankang!

【FPGA】day16-FIFO实现uart协议

在keras中使用gpu加速训练模型;安装cuda;cudnn;cudnn_cnn_infer64_8.dll 不在path中;device_lib.list_local_devices无gpu;挂掉

【FPGA】day15-串口协议uart回环工程

LeetCode 530: 二叉搜索树的最小绝对差

What are the good techniques for domestic spot silver: simple usage of common indicators
Beyond compare 3 key serial number sharing and key revocation solutions

Free batch import software for generating sitemap maps
随机推荐
基于OpenVINO部署的工业缺陷检测产业实践范例实战
你不会还在用Xshell吧?这款开源的终端工具才是yyds!
Custom action logging annotation
【图像分类案例】(10) Vision Transformer 动物图像三分类,附Pytorch完整代码
LeetCode 98: 验证二叉搜索树
【FPGA】day16-FIFO实现uart协议
【ICLR 2022】Towards Continual Knowledge Learning of Language Models
【LeetCode】221.最大正方形
Hoo Hufu Research Institute | houlang in the coin circle - gamefi like baokemeng: evoverses
Application of discrete time integration in Simulink simulation
Nodejs报内部错误 TypeError: Cannot read property ‘destroy‘ of undefined的解决方法
985毕业,35岁创业失败,36岁回炉40岁被裁,中年夫妻无业咋办?
The calculation of Luogu p1028 number and the introduction to dynamic programming
Luogu p2657 [scoi2009]windy number problem solving digit DP
Regular expression does not contain a string
Batch downloading of pictures + mosaic of pictures: multiple pictures constitute the Dragon Boat Festival Ankang
Node red series (II and III): develop a Gaode map panel dashboard in node red
[no title] 416 Split equal sum subset
51单片机学习记录(六)IIC,ADC部分
[apprendre la programmation FPGA à partir de zéro - 16]: chapitre de démarrage rapide - Étapes 2 - 4 - Verilog HDL Language Description Language Basic Syntax (both Software programmers and Hardware En