Leetcode-236. Lowest Common Ancestor of a Binary Tree 最小公共祖先
来源:互联网 发布:jenkins 自动编译java 编辑:程序博客网 时间:2024/05/29 15:21
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对应的root->....->p和root->....->q的路径,然后比较两个路径,找到最后一公共节点即可。
class Solution {public: TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) { if(!root || root == p || root == q) return root; vector<TreeNode*> s,t; findPath(root, p, s); findPath(root, q, t); int k = min(s.size(), t.size()), i = 1; for(;i<k;++i){ if(s[i] != t[i]) break; } return s[i-1]; } bool findPath(TreeNode* root, TreeNode* node, vector<TreeNode*>& path){ if (root == NULL) return false; path.push_back(root); if (root == node) return true; if (root->left && findPath(root->left, node, path) || root->right && findPath(root->right, node, path)) return true; path.pop_back(); return false; }};
第二种思路:递归寻找。如果p,q节点有一个是当前root节点,那就直接返回root节点,如果不是,就在左右子树中寻找p,q。如果在一个子树中找到p,另个子树找到q,那么当前root就是最小的公共祖先。如果都在同一个子树上,那就递归在这个子树中寻找p,q的公共祖先。代码非常简单。
class Solution {public: TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) { if (!root || root == p || root == q) return root; TreeNode* left = lowestCommonAncestor(root->left, p, q); TreeNode* right = lowestCommonAncestor(root->right, p, q); return !left ? right : !right ? left : root; }};
后:不知道p,q中如果有一个点不在树中,这种情况有没有考虑过。好像方法二没对这种情况进行讨论。方法一的话对应bool的函数进行一个判断即可。另外方法一的话可以解决二叉树中两个节点之间最短路径这类问题。
- Leetcode-236. Lowest Common Ancestor of a Binary Tree 最小公共祖先
- [Leetcode] Lowest Common Ancestor of a Binary Tree 最小公共祖先
- LeetCode 236. Lowest Common Ancestor of a Binary Tree(二叉树的最低公共祖先)
- leetcode之二叉树类之最小公共祖先-----236/235. Lowest Common Ancestor of a Binary/Binary Search Tree
- 最低公共祖先Lowest Common Ancestor of a Binary Tree
- LeetCode 235 Lowest Common Ancestor of a Binary Search Tree(二叉搜索树的最小公共祖先)
- Lowest Common Ancestor of a Binary Search Tree(二叉树最小公共祖先)
- leetcode 235. Lowest Common Ancestor of a Binary Search Tree 最近公共祖先 + BST
- 236. Lowest Common Ancestor of a Binary Tree最长公共祖先
- leetcode 236. Lowest Common Ancestor of a Binary Tree 最近公告祖先LCA + 二叉树
- LeetCode 235. Lowest Common Ancestor of a Binary Search Tree(二叉搜索树的最低公共祖先)
- Lowest Common Ancestor of a Binary Search Tree 递归寻找二叉查找树公共祖先
- Lowest Common Ancestor of a Binary Tree 二叉树的公共祖先
- 【二叉树】最近公共祖先Lowest Common Ancestor of a Binary Tree
- 【二叉树】最近公共祖先【235. Lowest Common Ancestor of a Binary Search Tree】
- [leetcode] 236.Lowest Common Ancestor of a Binary Tree
- [LeetCode]236.Lowest Common Ancestor of a Binary Tree
- [leetcode] 236.Lowest Common Ancestor of a Binary Tree
- WebLogic使用总结(四)——WebLogic部署Web应用
- MarkdownPad2注册码2017.09.05测试通过
- SQL HAVING 子句
- JQ AJAX
- Android 广播
- Leetcode-236. Lowest Common Ancestor of a Binary Tree 最小公共祖先
- Spring Boot -- Swagger之Hello Wrold
- VC++6.0使用MATCOM矩阵库的方法
- 架设部署CDN
- CSS 动画实战:创建一个漂亮的加载动画
- WebLogic使用总结(五)——Web项目使用Sigar在WebLogic服务器部署遇到的问题
- 如何防止脏读、不可重复读、幻读
- Win10无法安装SQLServer ,无法安装.net35框架的终极解决办法
- 在controller层,控制是否返回带layout