[LeetCode] Lowest Common Ancestor of a Binary Tree
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了[LeetCode] Lowest Common Ancestor of a Binary Tree,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2698字,纯文字阅读大概需要4分钟。
内容图文
![[LeetCode] Lowest Common Ancestor of a Binary Tree](/upload/InfoBanner/zyjiaocheng/1094/620502700c0a46e29d110a0942b51bf0.jpg)
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.
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 bool getPath(TreeNode *root, TreeNode *p, vector<TreeNode *> &path) { 13if (root == NULL) returnfalse; 14 path.push_back(root); 15if (root == p) returntrue; 16if (getPath(root->left, p, path) || getPath(root->right, p, path)) returntrue; 17 path.pop_back(); 18returnfalse; 19 } 2021 TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) { 22 vector<TreeNode*> path1, path2; 23 getPath(root, p, path1); 24 getPath(root, q, path2); 25 TreeNode *res = root; 26int idx = 0; 27while (idx < path1.size() && idx < path2.size()) { 28if (path1[idx] != path2[idx]) break; 29else res = path1[idx++]; 30 } 31return res; 32 } 33 };
跟上面一个意思。
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 void getPath(TreeNode *root, TreeNode *p, TreeNode *q, vector<TreeNode*> &path, vector<TreeNode *> &path1, vector<TreeNode*> &path2) { 13if (root == NULL) return; 14 path.push_back(root); 15if (root == p) path1 = path; 16if (root == q) path2 = path; 17//找到两个节点后就可以退出了18if (!path1.empty() && !path2.empty()) return; 19 getPath(root->left, p, q, path, path1, path2); 20 getPath(root->right, p, q, path, path1, path2); 21 path.pop_back(); 22 } 2324 TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) { 25 vector<TreeNode*> path, path1, path2; 26 getPath(root, p, q, path, path1, path2); 27 TreeNode *res = root; 28int idx = 0; 29while (idx < path1.size() && idx < path2.size()) { 30if (path1[idx] != path2[idx]) break; 31else res = path1[idx++]; 32 } 33return res; 34 } 35 };
原文:http://www.cnblogs.com/easonliu/p/4643873.html
内容总结
以上是互联网集市为您收集整理的[LeetCode] Lowest Common Ancestor of a Binary Tree全部内容,希望文章能够帮你解决[LeetCode] Lowest Common Ancestor of a Binary Tree所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。