首页 > 其他 > 详细

[2015/7/17] #235, #236

时间:2015-07-19 13:21:52      阅读:117      评论:0      收藏:0      [点我收藏+]

#235 Lowest Common Ancestor of a Binary Search Tree

Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.

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).”

        _______6______
       /                  ___2__          ___8__
   /      \        /         0      _4       7       9
         /           3   5

For example, the lowest common ancestor (LCA) of nodes 2 and 8 is 6. Another example is LCA of nodes 2 and 4 is 2, since a node can be a descendant of itself according to the LCA definition.

The best code so far:

 1 /**
 2  * Definition for a binary tree node.
 3  * struct TreeNode {
 4  *     int val;
 5  *     TreeNode *left;
 6  *     TreeNode *right;
 7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 8  * };
 9  */
10 class Solution {
11 public:
12     TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
13         TreeNode *cur = root;
14         while (cur != NULL) {
15             if (cur->val > p->val && cur->val > q->val) cur = cur->left;
16             else if (cur->val < p->val && cur->val < q->val) cur = cur->right;
17             else return cur;
18         }
19         return cur;
20     }
21 };

http://www.cnblogs.com/easonliu/p/4639043.html

不用迭代效率显然要高啊!


 

Code by S:

 1 /**
 2  * Definition for a binary tree node.
 3  * struct TreeNode {
 4  *     int val;
 5  *     TreeNode *left;
 6  *     TreeNode *right;
 7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 8  * };
 9  */
10 class Solution {
11 public:
12     TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
13         if(root == NULL || p == NULL || q == NULL)
14             return NULL;
15         /*TreeNode* tmp;
16         if (p->val == q->val)
17             return p;
18         if (p->val > q->val){
19             tmp = p;
20             p = q;
21             q = tmp;
22             delete tmp;
23         }*/
24 
25         if((p->val < root->val) && (q->val < root->val)){
26             root = root->left;
27             return lowestCommonAncestor(root, p, q);
28         }
29         else if((p->val > root->val) && (q->val > root->val)){
30             root = root->right;
31             return lowestCommonAncestor(root, p, q);
32         }
33         else
34             return root;
35     }
36 };

默认p->val <= q->val; 一开始没考虑NULL的情况,以及一开始迭代初每次判断一左一右则终止这个,一直报runtime error的错,最后把这个判断放在else里面就跑过了。

 

 



#237 Lowest Common Ancestor of a Binary Tree

Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.

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).”

        _______3______
       /                  ___5__          ___1__
   /      \        /         6      _2       0       8
         /           7   4

For example, the lowest common ancestor (LCA) of nodes 5 and 1 is 3. Another example is LCA of nodes 5 and 4 is 5, since a node can be a descendant of itself according to the LCA definition.

The best code so far

http://wp.javayu.me/2014/02/lowest-common-ancestor-of-a-binary-tree/

http://articles.leetcode.com/2011/07/lowest-common-ancestor-of-a-binary-tree-part-i.html

http://blog.csdn.net/xudli/article/details/8560362


 

Code by S:

 1 /**
 2  * Definition for a binary tree node.
 3  * struct TreeNode {
 4  *     int val;
 5  *     TreeNode *left;
 6  *     TreeNode *right;
 7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 8  * };
 9  */
10 class Solution {
11 public:
12     TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
13         if(root == NULL || p == NULL || q == NULL)
14             return NULL;
15         if(root == p || root == q)
16             return root;
17         TreeNode *L = lowestCommonAncestor(root->left, p, q);
18         TreeNode *R = lowestCommonAncestor(root->right, p, q);
19         if(L && R)
20             return root;
21         return L ? L : R;
22         }
23 };

想通道理都花了好久- -,被自己蠢到了。

 

[2015/7/17] #235, #236

原文:http://www.cnblogs.com/leetcodegroup/p/4654116.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!