当前位置:网站首页>Leetcode brush question 897 incremental sequential search tree
Leetcode brush question 897 incremental sequential search tree
2022-04-23 13:45:00 【Du Xiaorui】
Finally ended a few days of dynamic planning , Today is a simple question . But take a closer look , It seems that this simple problem related to trees is not so simple , At least for me . For so long , Little has been done on the subject of trees , Just take today to learn .
Title Description

Title address : Incremental sequential search tree
Solution 1
The idea of solution one is relatively simple and direct , Is to traverse directly in middle order, and then store all elements in an array , Then take out the elements from the array and rebuild a tree .
# 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 increasingBST(self, root: TreeNode) -> TreeNode:
def dfs(root: TreeNode, ans: List) -> List:
if root == None:return
dfs(root.left, ans)
ans.append(root.val)
dfs(root.right, ans)
nums = []
dfs(root, nums)
dummynode = TreeNode(-1)
currentnode = dummynode
for i in nums:
currentnode.left = None
currentnode.right = TreeNode(i)
currentnode = currentnode.right
return dummynode.right
Dumb nodes are used here again , Dumb nodes are very useful in many topics related to linked lists and trees , Some complicated boundary judgment can be omitted .
Solution 2
Solution 2 is also the method I first thought of , That is to directly change the pointer during the middle order traversal . But I didn't do it myself , Read the answer to understand how to do .
# 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 increasingBST(self, root: TreeNode) -> TreeNode:
dummynode = TreeNode(-1)
self.currentnode = dummynode
self.dfs(root)
return dummynode.right
def dfs(self, node: TreeNode) -> List:
if node == None:return
self.dfs(node.left)
self.currentnode.right = node
node.left = None
self.currentnode = node
self.dfs(node.right)
版权声明
本文为[Du Xiaorui]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230556585135.html
边栏推荐
- [point cloud series] Introduction to scene recognition
- Isparta is a tool that generates webp, GIF and apng from PNG and supports the transformation of webp, GIF and apng
- GDB的使用
- Lenovo Saver y9000x 2020
- JS compares different elements in two arrays
- 爱可可AI前沿推介 (4.23)
- Oracle lock table query and unlocking method
- Exemple de méthode de réalisation de l'action d'usinage à point fixe basée sur l'interruption de déclenchement du compteur à grande vitesse ob40 pendant le voyage de tia Expo
- [multi screen interaction] realize dual multi screen display II: startactivity mode
- Ai21 labs | standing on the shoulders of giant frozen language models
猜你喜欢

切线空间(tangent space)

Lenovo Saver y9000x 2020

SAP UI5 应用开发教程之七十二 - SAP UI5 页面路由的动画效果设置试读版

Static interface method calls are not supported at language level '5'
![[point cloud series] neural opportunity point cloud (NOPC)](/img/ba/24417762ef5c52d2d6b57f18a3b0a2.png)
[point cloud series] neural opportunity point cloud (NOPC)

Double pointer instrument panel reading (I)

Search ideas and cases of large amount of Oracle redo log

Unified task distribution scheduling execution framework

TIA博途中基于高速计数器触发中断OB40实现定点加工动作的具体方法示例
![[point cloud series] foldingnet: point cloud auto encoder via deep grid deformation](/img/c0/cc29ae6948dbe42954cd9da326ef77.png)
[point cloud series] foldingnet: point cloud auto encoder via deep grid deformation
随机推荐
The query did not generate a result set exception resolution when the dolphin scheduler schedules the SQL task to create a table
[point cloud series] multi view neural human rendering (NHR)
切线空间(tangent space)
[tensorflow] sharing mechanism
Two ways to deal with conflicting data in MySQL and PG Libraries
Dolphin scheduler integrates Flink task pit records
面试官给我挖坑:URI中的 “//” 有什么用?
[Video] Bayesian inference in linear regression and R language prediction of workers' wage data | data sharing
[point cloud series] full revolutionary geometric features
集简云 x 飞书深诺,助力企业运营部实现自动化办公
Solution: you have 18 unapplied migration (s) Your project may not work properly until you apply
Test on the time required for Oracle to delete data with delete
PG SQL intercepts the string to the specified character position
Resolution: argument 'radius' is required to be an integer
ARGB transparency conversion
解决tp6下载报错Could not find package topthink/think with stability stable.
Opening: identification of double pointer instrument panel
Publish custom plug-ins to local server
Oracle defines self incrementing primary keys through triggers and sequences, and sets a scheduled task to insert a piece of data into the target table every second
JUC interview questions about synchronized, ThreadLocal, thread pool and atomic atomic classes