Skip to content

Commit cd40d74

Browse files
authored
Update Readme.md
1 parent 56b5ccd commit cd40d74

File tree

1 file changed

+3
-1
lines changed
  • Tree/297.Serialize-and-Deserialize-Binary-Tree

1 file changed

+3
-1
lines changed

Tree/297.Serialize-and-Deserialize-Binary-Tree/Readme.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@
4646
return root;
4747
}
4848
```
49-
需要额外一个getSize函数来计算这个子树共有多少个元素。
49+
需要额外一个getSize函数来计算这个子树共有多少个元素。或者在DFS的返回值里添加一个信息,表示该子树下的节点总数。
5050

51+
#### 解法3:
52+
在解法2的基础上,有一个技巧可以避免额外调用getSize. 我们将所有节点都放入一个队列之中。每次的DFS其实都是从该队列中弹出首元素作为子树的根;然后构建左子树;再构建右子树。当我们试图构建右子树的时候,其实队列中的首元素就已经是右子树的根了。
5153

5254
[Leetcode Link](https://leetcode.com/problems/serialize-and-deserialize-binary-tree)

0 commit comments

Comments
 (0)