当前位置:网站首页>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
边栏推荐
- TCP reset Gongji principle and actual combat reproduction
- [point cloud series] multi view neural human rendering (NHR)
- Oracle kills the executing SQL
- 19c RAC steps for modifying VIP and scanip - same network segment
- 19c environment ora-01035 login error handling
- Use future and countdownlatch to realize multithreading to execute multiple asynchronous tasks, and return results after all tasks are completed
- Android clear app cache
- Oracle RAC database instance startup exception analysis IPC send timeout
- 聯想拯救者Y9000X 2020
- Django::Did you install mysqlclient?
猜你喜欢
Unified task distribution scheduling execution framework
[Journal Conference Series] IEEE series template download guide
TIA博途中基於高速計數器觸發中斷OB40實現定點加工動作的具體方法示例
Usereducer basic usage
10g database cannot be started when using large memory host
kettle庖丁解牛第16篇之输入组件周边讲解
Dolphin scheduler integrates Flink task pit records
[point cloud series] full revolutionary geometric features
Short name of common UI control
Detailed explanation of constraints of Oracle table
随机推荐
[point cloud series] unsupervised multi task feature learning on point clouds
Operations related to Oracle partition
Dolphin scheduler integrates Flink task pit records
Use of GDB
Oracle creates tablespaces and modifies user default tablespaces
Aicoco AI frontier promotion (4.23)
The difference between is and as in Oracle stored procedure
Troubleshooting of expdp export error when Oracle table has logical bad blocks
pycharm Install packages failed
Information: 2021 / 9 / 29 10:01 - build completed with 1 error and 0 warnings in 11S 30ms error exception handling
19c RAC steps for modifying VIP and scanip - same network segment
19c environment ora-01035 login error handling
Failure to connect due to improper parameter setting of Rac environment database node. Troubleshooting
Analysis of redo log generated by select command
Remove the status bar
Double pointer instrument panel reading (I)
Example of specific method for TIA to trigger interrupt ob40 based on high-speed counter to realize fixed-point machining action
聯想拯救者Y9000X 2020
Two ways to deal with conflicting data in MySQL and PG Libraries
AI21 Labs | Standing on the Shoulders of Giant Frozen Language Models(站在巨大的冷冻语言模型的肩膀上)