<LeetCode OJ> 235. / 236. LCA of a BSTree & BTree
来源:互联网 发布:javascript 提交表单 编辑:程序博客网 时间:2024/06/05 21:10
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.
分析:
本问题属于找规律的问题!
注意:本题给定的两个节点一定在这个二叉搜索树上,所以一定存在最低公共祖先。
如果当前节点r是q和p的祖先,一定满足如下条件:
1)p和q分居两颗子树
2)p和q中有一个就是当前节点,另一个居于某颗子树
那么其他情况呢?
3)p和q都在左子树或者右子树
而根据二叉搜索树的规则,我们可以通过比较三个节点的大小来判断谁在谁的那一边
class Solution {public: TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) { if(root->val < p->val && root->val < q->val)//都在右子树上 return lowestCommonAncestor(root->right,p,q); else if(root->val >p->val && root->val > q->val) //都在左子树上 return lowestCommonAncestor(root->left,p,q); else return root; }};
或者写成迭代的形式:
class Solution {public: TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) { TreeNode* curAnc=root; while(curAnc) { if(curAnc->val < p->val && curAnc->val < q->val)//都在右子树上 curAnc=curAnc->right; else if(curAnc->val >p->val && curAnc->val > q->val) //都在左子树上 curAnc=curAnc->left; else return curAnc; } }};
tips:
上面这两种写法,不允许两个给定节点不存在于二叉搜索树上或者一个存在而另一个不存在。
也没做什么特殊处理啊?尽然超过了这么多leetcode玩家
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.
分析:
注意:本题给定的两个节点一定在这个二叉树上,所以一定存在最低公共祖先。
自己的答案:(纯手动,调试了半天才搞对)
思路就是记录从根节点达到p和q的两条路径,显然知道了两条路径之后,不相同的上一个节点就是最低公共祖先。
比如4和2的:
4的路径:3,5,2,4
2的路径:3,5,2
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public: void getPath(TreeNode* node, TreeNode* q,TreeNode* p,vector<TreeNode*> &result)//不要用值传递不然内存会大爆炸 { if(!qpath.empty() && !ppath.empty()) return; result.push_back(node); if(node==q) qpath=result; if(node==p) ppath=result; if(node->left) getPath(node->left,q,p,result); if(node->right) getPath(node->right,q,p,result); result.pop_back();//回溯去掉, } TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) { if(!root || !p || !q) return NULL; vector<TreeNode*> result;//找出两个包含这两个结点的路径 getPath(root,q,p,result); int i=1,j=1; while(i<qpath.size() && j<ppath.size()) { if(ppath[i]!=qpath[j]) return ppath[i-1]; if(i==qpath.size()-1)//这些返回条件一定要写清楚..........累觉不爱 return qpath[i]; if(j==ppath.size()-1) return ppath[j]; i++;j++; } return ppath[0]; } private: vector<TreeNode*> qpath; vector<TreeNode*> ppath;};
注:本博文为EbowTang原创,后续可能继续更新本文。如果转载,请务必复制本条信息!
原文地址:http://blog.csdn.net/ebowtang/article/details/50708367
原作者博客:http://blog.csdn.net/ebowtang
- <LeetCode OJ> 235. / 236. LCA of a BSTree & BTree
- leetcode 236. Lowest Common Ancestor of a Binary Tree 最近公告祖先LCA + 二叉树
- LeetCode 236 Lowest Common Ancestor of a Binary Tree (LCA)
- LeetCode OJ:Letter Combinations of a Phone Number
- LeetCode OJ - Letter Combinations of a Phone Number
- LeetCode OJ Letter Combinations of a Phone Number
- LeetCode OJ 之 Lowest Common Ancestor of a Binary Tree
- LeetCode OJ - Letter Combinations of a Phone Number
- LeetCode OJ-17-Letter Combinations of a Phone Number
- leetcode oj java Number of Segments in a String
- LeetCode OJ-17.Letter Combinations of a Phone Number(DFS)
- 【leetcode】二叉树的LCA问题:Lowest Common Ancestor of a Binary Tree
- LeetCode OJ Number of Islands
- LeetCode OJ算法题(十八):Letter Combinations of a Phone Number
- LeetCode OJ 之 Lowest Common Ancestor of a Binary Search Tree
- LeetCode Lowest Common Ancestor of a Binary Search Tree 刷题 OJ 算法
- [leetcode oj 235]Lowest Common Ancestor of a Binary Search Tree
- <LeetCode OJ> 345 / 344 Reverse Vowels of a String / Reverse String
- codevs1222信与信封问题
- 国际化与全球化的区别
- 获取WEB项目的不同路径的方法
- 使用Python对Excel进行读写操作
- 深入理解计算机系统-之-数值存储(四)--整数在内存中的存储方式
- <LeetCode OJ> 235. / 236. LCA of a BSTree & BTree
- JAVA多线程-Lock的使用(四)-读写锁ReentrantReadWriteLock
- Android机器视觉实现框架
- Main方法格式讲解
- UWP开发入门系列笔记之(零):UWP的前世今生
- android中使用RecyclerView
- Struts2标签库(四)之非表单标签
- Diksam代码阅读(3)——Diksam的基础结构
- 下拉刷新