<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    IT技術小屋

    秋風秋雨,皆入我心

      BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
      38 隨筆 :: 1 文章 :: 19 評論 :: 0 Trackbacks
    Lowest Common Ancestor of a Binary Search Tree (BST)
    Given a binary search tree (BST), find the lowest common ancestor of two given nodes in the BST.
           _______6______
          /              \
       ___2__          ___8__
      /      \        /      \
      0      _4       7       9
            /  \
            3   5
    Using the above tree as an example, the lowest common ancestor (LCA) of nodes 2 and 8 is 6. 
    But how about LCA of nodes 2 and 4? Should it be 6 or 2?
    According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between 
    two nodes v and w as the lowest node in T that has both v and w as descendants (where we allow a 
    node to be a descendant of itself).” Since a node can be a descendant of itself, the LCA of 2 and 
    4 should be 2, according to this definition.
    Hint:
    A top-down walk from the root of the tree is enough.

     1 public class LowestCommonAncestorOfaBinarySearchTree {
     2     public TreeNode LCA(TreeNode root, TreeNode p, TreeNode q) {
     3         if (root == null || p == null || q == null)
     4             return null;
     5         if (Math.max(p.val, q.val) < root.val)
     6             return LCA(root.left, p, q);
     7         if (Math.min(p.val, q.val) > root.val)
     8             return LCA(root.right, p, q);
     9         return root;
    10     }
    11 }


    Given a binary tree, find the lowest common ancestor of two given nodes in the tree.
     
     
            _______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.
     
    Hint:
    Top-down or bottom-up? Consider both approaches and see which one is more efficient.
     1 public class LowestCommonAncestorOfBinaryTree {
     2     public TreeNode LCA(TreeNode root, TreeNode p, TreeNode q) {
     3         if (root == null)
     4             return null;
     5         if (root == p || root == q)
     6             return root;
     7         TreeNode left = LCA(root.left, p, q);
     8         TreeNode right = LCA(root.right, p, q);
     9         if (left != null && right != null)
    10             return root;
    11         return left != null ? left : right;
    12     }
    13 }
    posted on 2014-01-06 09:32 Meng Lee 閱讀(345) 評論(0)  編輯  收藏 所屬分類: Leetcode
    主站蜘蛛池模板: 亚洲综合久久综合激情久久| 相泽亚洲一区中文字幕| 亚洲成人激情在线| 日韩精品无码免费专区午夜不卡| 亚洲乱码国产一区网址| 老妇激情毛片免费| 亚洲第一福利网站在线观看| 美女视频黄a视频全免费网站一区| 女性无套免费网站在线看| 亚洲熟女www一区二区三区| 拨牐拨牐x8免费| 真人无码作爱免费视频| 亚洲精品456播放| 国产色爽免费无码视频| 亚洲成AV人片在线观看无码 | 亚洲国产女人aaa毛片在线| 久久久久国产精品免费网站| 亚洲精品日韩专区silk| 一个人在线观看视频免费| 亚洲永久网址在线观看| 亚洲高清视频一视频二视频三| 久久久久国色AV免费观看| 久久精品国产亚洲综合色| 在线观看免费av网站| 国产精品亚洲综合久久| 国产国产人免费视频成69大陆| 无人视频在线观看免费播放影院| 亚洲一级特黄大片无码毛片 | 国内永久免费crm系统z在线 | 亚洲精品在线观看视频| 希望影院高清免费观看视频| 色窝窝亚洲AV网在线观看| 亚洲色大成网站www永久一区| 99精品免费观看| 午夜亚洲国产理论片二级港台二级| 亚洲精品国产福利一二区| 亚欧人成精品免费观看| 亚洲AV无码一区二区乱子仑| 日本亚洲视频在线| 我想看一级毛片免费的| baoyu116.永久免费视频|