现在的位置: 首页 > 综合 > 正文

ch4.6: find the LCA of 2 nodes in a binary tree with 3 different methods

2014年07月07日 ⁄ 综合 ⁄ 共 476字 ⁄ 字号 评论关闭

In this problem, it is important to fully understand the definition of least common ancestor of 2 nodes:

it means: (from leetcode blog:
点击打开链接
)

        _______3______
       /              \
    ___5__          ___1__
   /      \        /      \
   6      _2       0       8
         /  \
         7   4

If you are not so sure about the definition of lowest common ancestor (LCA), please refer to my previous post:Lowest
Common Ancestor of a Binary Search Tree (BST)
 or the definition of LCA here.
Using the tree above as an example, the LCA of nodes 5 and 1 is 3.
Please note that LCA for nodes 5 and 4 is 5.

Here is the whole code with comments. Really good design by Hawstein.

抱歉!评论已关闭.