码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode Day3

时间:2015-08-08 13:19:45      阅读:106      评论:0      收藏:0      [点我收藏+]

标签:

 Lowest Common Ancestor of a Binary Search Tree

技术分享

 

技术分享
  1 import java.util.ArrayList;
  2 import java.util.List;
  3 
  4 /**
  5  * LeetCode: Lowest Common Ancestor of a Binary Search Tree 
  6  * Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST
  7  * 
  8  * @author LuoPeng
  9  * @time 215.8.5
 10  *
 11  */
 12 public class LowestCommonAncestor {
 13 
 14     /**
 15      * If a node A is the common ancestor, and its left child and right child are not at the same time.
 16      * A is the Lowest Common Ancestor
 17      * 
 18      * @param root the root of the tree
 19      * @param p 
 20      * @param q 
 21      * @return lowest common ancestor (LCA) of p and q
 22      */
 23     public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
 24         
 25         if ( root == null || p == root || q == root) {return root;}
 26         
 27         TreeNode lca = null;
 28         /*
 29          * If one child is null, the lowest common ancestor must be the child of the other child of root
 30          */
 31         if ( root.left == null) {
 32             lca = lowestCommonAncestor(root.right, p, q);
 33         } else if ( root.right == null) {
 34             lca = lowestCommonAncestor(root.left, p, q);
 35         } else {
 36             boolean first = isCommonAncestor(root.left, p, q);
 37             boolean second = isCommonAncestor(root.right, p, q);
 38             if ( first) {
 39                 // if root.left is a common ancestor, the LCA must be root.left or a child of it.
 40                 lca = lowestCommonAncestor(root.left, p, q);
 41             } else if (second) {
 42                 // if root.right is a common ancestor, the LCA must be root.right or a child of it.
 43                 lca = lowestCommonAncestor(root.right, p, q);
 44             } else {
 45                 // For root is a common ancestor of p and q, the LCA must be root if the left child
 46                 // and right child are not the common ancestors.
 47                 lca = root;
 48             }
 49         }
 50         
 51         return lca;
 52     }
 53     
 54     /**
 55      * Whether root is the common ancestor of p and q 
 56      * 
 57      * @param root a node
 58      * @param p a node
 59      * @param q a node
 60      * @return True if root is the common ancestor of p and q, otherwise false.
 61      */
 62     private boolean isCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
 63         if ( root == null) {return false;}
 64         
 65         TempQueue queue = new TempQueue();
 66         TreeNode temp = null;
 67         boolean first = false;
 68         boolean second = false;
 69         
 70         // Breadth First Search
 71         queue.push(root);
 72         while ( !queue.empty()) {
 73             temp = queue.peek();
 74             queue.pop();
 75             if ( temp ==p) {
 76                 first = true;
 77             } else if ( temp == q) {
 78                 second = true;
 79             }
 80             // add the child
 81             if ( temp.left != null) {
 82                 queue.push(temp.left);
 83             }
 84             if ( temp.right != null) {
 85                 queue.push(temp.right);
 86             }
 87             // break if p and q have bean found
 88             if ( first && second) {
 89                 break;
 90             }
 91         }
 92         
 93         return first && second;
 94     }
 95     
 96 }
 97 
 98 /**
 99  * Queue
100  *
101  */
102 class TempQueue {
103     public void push(TreeNode x) {
104         values.add(x);
105     }
106 
107     public void pop() {
108         values.remove(0);
109     }
110 
111     public TreeNode peek() {
112         return values.get(0);
113     }
114 
115     public int size() {
116         return values.size();
117     }
118     
119     public boolean empty() {
120         return values.size()==0;
121     }
122     
123     private List<TreeNode> values = new ArrayList<TreeNode>();
124 }
View Code

 

LeetCode Day3

标签:

原文地址:http://www.cnblogs.com/luop/p/4712853.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!