We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent db9b988 commit c64ea64Copy full SHA for c64ea64
Tree/2096.Step-By-Step-Directions-From-a-Binary-Tree-Node-to-Another/Readme.md
@@ -0,0 +1,5 @@
1
+### 2096.Step-By-Step-Directions-From-a-Binary-Tree-Node-to-Another
2
+
3
+本题和以往求LCA的题目不同。以往的LCA只需要求得LCA的节点本身,而这道题需要设计到路径打印。因此我们就老老实实地用递归的方法,把从root到q或者q的路径先记录出来,然后再比较得到LCA的位置。
4
5
+注意此题中我们需要记录两个信息,首先是从root到p(或者q)一路上遇到的节点数值,另外是一路上的操作(L或者R)。假设两条路径dirs1和dirs2在第k个节点位置分叉(也就是LCA的位置)。根据题意,我们就将dirs1[k:]全部搞成“U”(因为从p到LCA一路都是up),再拼接上dirs2[k:]这段路径即可。
0 commit comments