zoukankan      html  css  js  c++  java
  • [LintCode] 474 Lowest Common Ancestor II 解题报告

    Description
    Given the root and two nodes in a Binary Tree. Find the lowest common ancestor(LCA) of the two nodes.

    The lowest common ancestor is the node with largest depth which is the ancestor of both nodes.

    The node has an extra attribute parent which point to the father of itself. The root's parent is null.


    Example
    For the following binary tree:

      4
     /
    3   7
         /
       5   6
    LCA(3, 5) = 4

    LCA(5, 6) = 7

    LCA(6, 7) = 7

    5/9/2017

    算法班

    未经验证

    思路,把向上遍历的node加到set里面,直到其中一个是null。此时直接返回root

    解法一,如果确定A,B都在tree当中:

     1 class Solution {
     2     public ParentTreeNode lowestCommonAncestorII(ParentTreeNode root,
     3                                                  ParentTreeNode A,
     4                                                  ParentTreeNode B) {
     5         // Write your code here   
     6         if (root == null) return null;
     7         Set<ParentTreeNode> s = new HashSet<ParentTreeNode>();
     8         ParentTreeNode tmpA = A;
     9         ParentTreeNode tmpB = B;
    10 
    11         while (tmpA != null && tmpB != null) {
    12             if (s.contains(tmpA)) return tmpA;
    13             if (s.contains(tmpB)) return tmpB;
    14 
    15             s.add(tmpA);
    16             s.add(tmpB);
    17 
    18             tmpA = tmpA.parent;
    19             tmpB = tmpB.parent;
    20         }
    21         return root;
    22     }
    23 }

    解法二,A,B未必在tree当中

     1 class Solution {
     2     public ParentTreeNode lowestCommonAncestorII(ParentTreeNode root,
     3                                                  ParentTreeNode A,
     4                                                  ParentTreeNode B) {
     5         // Write your code here   
     6         if (root == null) return null;
     7         Set<ParentTreeNode> s = new HashSet<ParentTreeNode>();
     8         ParentTreeNode tmpA = A;
     9         ParentTreeNode prevA = null;
    10         ParentTreeNode tmpB = B;
    11         ParentTreeNode prevB = null;
    12 
    13         while (tmpA != null || tmpB != null) {
    14             if (tmpA != null) {
    15                 if (s.contains(tmpA)) return tmpA;
    16                 s.add(tmpA);
    17                 prevA = tmpA;
    18                 tmpA = tmpA.parent;
    19             }
    20             if (tmpB != null) {
    21                 if (s.contains(tmpB)) return tmpB;
    22                 s.add(tmpB);
    23                 prevB = tmpB;
    24                 tmpB = tmpB.parent;
    25             }            
    26         }
    27         if (prevA == root && prevB == root) return root;
    28         return null;
    29     }
    30 }
  • 相关阅读:
    团队编程项目作业2-爬虫豆瓣top250项目代码设计规范
    《团队-爬取豆瓣电影TOP250-设计文档》
    个人编程作业1-GIT应用
    团队-爬取豆瓣Top250-开发环境搭建过程
    课后作业-阅读任务-阅读提问-1
    20170914-构建之法:现代软件工程-阅读笔记
    结对-贪吃蛇开发环境搭建过程
    结对-贪吃蛇游戏设计文档
    结对-结对编程项目作业名称-需求分析
    团队编程项目自我介绍
  • 原文地址:https://www.cnblogs.com/panini/p/6834830.html
Copyright © 2011-2022 走看看