“常用公式”在线计算,“设计手册”在线查询
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 分享淘帖 支持支持 反对反对

共 1 个关于本帖的回复 最后回复于 2013-7-30 16:11

沙发
新来的 发表于 2013-7-30 16:11:46 | 只看该作者
研发埠培训中心
Good Answer: There will always be exactly one path: from the starting node to thelowest common ancestor of the nodes to these condnode.The goalist o identify thelowest common ancestor.For each node,keep track of a set of nodes in the binary tree(using a hashtable or a BST)as well as a current node.At each iteration,for each of the two current nodes,change the current node to be its parent and add it to the appropriate set.The first element that is added to one set when it is already present in the other set is the lowest common ancestor. This algorithm takes O(n) time,where n is the length of the path.For example,if we were finding the lowest common ancestor of 3and 15 in the above tree,our algorithm wouldo the following:Current node 1 | Current node 2 | Set 1 |Set 2-------------------------------------------------------- 3 |15 | 3 | 15 6 |12 | 3, 6 | 15, 12 17 |6 | 3, 6, 17 | 15, 12, 6To improve the solution,we actually only need to use one set instead of two.
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注我们

360网站安全检测平台