File tree Expand file tree Collapse file tree 1 file changed +7
-2
lines changed Expand file tree Collapse file tree 1 file changed +7
-2
lines changed Original file line number Diff line number Diff line change @@ -199,17 +199,22 @@ class FindElements:
199199如果我们把树中的数全部加 1 会怎么样?
200200
201201![ ] ( https://tva1.sinaimg.cn/large/007S8ZIlly1ghluaaphnnj30rs0kuwhb.jpg )
202+
202203(图参考 https://leetcode.com/problems/find-elements-in-a-contaminated-binary-tree/discuss/431229/Python-Special-Way-for-find()-without-HashSet-O (1)-Space-O(logn)-Time)
203204
204205仔细观察发现,每一行的左右子树分别有不同的前缀:
205206
206207![ ] ( https://tva1.sinaimg.cn/large/007S8ZIlly1ghluackit8j312y0sgtdy.jpg )
207208
208- Ok,那么算法就来了。为了便于理解,我们来举个具体的例子,比如 target 是 9,我们首先将其加 1,二进制表示就是 1010。不考虑第一位,就是 010,我们只要:
209+ Ok,那么算法就来了,就是直接用 target 的二进制表示进行** 二叉树寻路** 即可。
210+
211+ 为了便于理解,我们来举个具体的例子,比如 target 是 9,我们首先将其加 1,二进制表示就是 1010。不考虑第一位,就是 010。
212+
213+ 我们只要:
209214
210215- 0 向左 👈
211216- 1 向右 👉
212- - - 0 向左 👈
217+ - 0 向左 👈
213218
214219就可以找到 9 了。
215220
You can’t perform that action at this time.
0 commit comments