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 閱讀(346) 評論(0)  編輯  收藏 所屬分類: Leetcode
          主站蜘蛛池模板: 宁晋县| 永康市| 巴东县| 商洛市| 文山县| 磐安县| 和政县| 嫩江县| 都安| 湘潭市| 灵台县| 永和县| 七台河市| 天等县| 青龙| 融水| 长阳| 玛纳斯县| 丰镇市| 商南县| 呈贡县| 荥阳市| 茶陵县| 嘉黎县| 普宁市| 泗阳县| 兴安盟| 屏山县| 仙游县| 和硕县| 姜堰市| 玛纳斯县| 大余县| 通城县| 土默特左旗| 河池市| 淮阳县| 镇坪县| 安庆市| 宜阳县| 吴忠市|