You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: app/docs/CommunityShare/Leetcode/538.把二叉搜索树转换为累加树_translated.md
+65-61Lines changed: 65 additions & 61 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,11 +1,12 @@
1
1
---
2
2
title: 538.Convert the binary search tree to cumulative tree
3
-
date: '2024.01.01 0:00'
3
+
date: "2024.01.01 0:00"
4
4
tags:
5
5
- - Python
6
6
- - answer
7
7
- - Binary tree
8
8
abbrlink: 32401b69
9
+
docId: wen0bbo8m93oih1mx6sva9sh
9
10
---
10
11
11
12
# topic:
@@ -86,73 +87,76 @@ There is a clever way to be online,只占用常数空间来实现中序Travers
86
87
2 3
87
88
/ \
88
89
4 5
89
-
90
90
```
91
+
91
92
1. Initialization The current node is the root node(current = 1)。
92
93
93
94
2. When the current node is not empty,Execute the following steps:
94
-
95
95
1. The left node of the current node is not empty。We find the front -drive node of the current node,也就是左子Tree中的最右节点。exist这个例子中,The front -drive node is the node5。
96
96
2. The right node of the front -drive node is empty,Point your right node to the current node(5 -> 1)。
97
97
3. Move the current node to its left child node(current = 2)。
98
-
```markdown
99
-
1
100
-
\
101
-
2
102
-
/ \
103
-
4 5
104
-
```
105
-
106
-
1. The left node of the current node is not empty。We find the front -drive node of the current node,也就是左子Tree中的最右节点。exist这个例子中,The front -drive node is the node4。
107
-
2. The right node of the front -drive node is empty,Point your right node to the current node(4 -> 2)。
108
-
3. Move the current node to its left child node(current = 4)。
109
-
```markdown
110
-
1
111
-
\
112
-
2
113
-
\
114
-
4
115
-
\
116
-
5
117
-
118
-
```
119
-
120
-
1. The left node of the current node is empty。Output当前节点的值(4)。
121
-
122
-
2. Move the current node to its right node(current = 5)。
123
-
124
-
3. The left node of the current node is empty。Output当前节点的值(5)。
125
-
126
-
4. Move the current node to its right node(current = 2)。
127
-
```markdown
128
-
1
129
-
\
130
-
2
131
-
\
132
-
4
133
-
```
134
-
1. The left node of the current node is empty。Output当前节点的值(2)。
135
-
2. Move the current node to its right node(current = 1)。
136
-
```markdown
137
-
1
138
-
\
139
-
2
140
-
```
141
-
1. The left node of the current node is empty。Output当前节点的值(1)。
142
-
2. Move the current node to its right node(current = 3)。
143
-
```markdown
144
-
1
145
-
\
146
-
2
147
-
```
148
-
1. The left node of the current node is empty。Output当前节点的值(3)。
149
-
2. Move the current node to its right node(current = null)。
150
-
3. The current node is empty,Traversal结束。
151
-
4.
152
-
According to the above steps,通过修改节点between的指针关系,我们完成了对Binary tree的中序Traversal。
153
98
99
+
```markdown
100
+
1
101
+
\
102
+
2
103
+
/ \
104
+
4 5
105
+
```
106
+
107
+
1. The left node of the current node is not empty。We find the front -drive node of the current node,也就是左子Tree中的最右节点。exist这个例子中,The front -drive node is the node4。
108
+
2. The right node of the front -drive node is empty,Point your right node to the current node(4 -> 2)。
109
+
3. Move the current node to its left child node(current = 4)。
110
+
111
+
```markdown
112
+
1
113
+
\
114
+
2
115
+
\
116
+
4
117
+
\
118
+
5
119
+
```
120
+
121
+
1. The left node of the current node is empty。Output当前节点的值(4)。
122
+
2. Move the current node to its right node(current = 5)。
123
+
3. The left node of the current node is empty。Output当前节点的值(5)。
124
+
4. Move the current node to its right node(current = 2)。
125
+
126
+
```markdown
127
+
1
128
+
\
129
+
2
130
+
\
131
+
4
132
+
```
133
+
134
+
1. The left node of the current node is empty。Output当前节点的值(2)。
135
+
2. Move the current node to its right node(current = 1)。
136
+
137
+
```markdown
138
+
1
139
+
\
140
+
2
141
+
```
142
+
143
+
1. The left node of the current node is empty。Output当前节点的值(1)。
144
+
2. Move the current node to its right node(current = 3)。
145
+
146
+
```markdown
147
+
1
148
+
\
149
+
2
150
+
```
151
+
152
+
1. The left node of the current node is empty。Output当前节点的值(3)。
153
+
2. Move the current node to its right node(current = null)。
154
+
155
+
3. The current node is empty,Traversal结束。
156
+
4. According to the above steps,通过修改节点between的指针关系,我们完成了对Binary tree的中序Traversal。
154
157
155
158
# Code:
159
+
156
160
```python 中序Traversal
157
161
classSolution:
158
162
defconvertBST(self, root: TreeNode) -> TreeNode:
@@ -163,7 +167,7 @@ class Solution:
163
167
total += root.val
164
168
root.val = total
165
169
dfs(root.left)
166
-
170
+
167
171
total =0
168
172
dfs(root)
169
173
return root
@@ -178,7 +182,7 @@ class Solution:
178
182
while succ.left and succ.left != node:
179
183
succ = succ.left
180
184
return succ
181
-
185
+
182
186
total =0# 记录累加的节点值之and
183
187
node = root # The current node initialization is the root node
0 commit comments