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

  1. _______3______
  2. / \
  3. ___5__ ___1__
  4. / \ / \
  5. 6 _2 0 8
  6. / \
  7. 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.

Subscribe to see which companies asked this question

非递归实现:

  1. TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
  2. if (root == nullptr)
  3. return root;
  4. stack<TreeNode*> sta;
  5. vector<TreeNode*> vec;
  6. bool tag1 = false;
  7. bool tag2 = false;
  8. sta.push(root);
  9. TreeNode* lastRoot = root;
  10. while (!sta.empty())
  11. {
  12. root = sta.top();
  13. if (root == p) {
  14. if(tag1 == false && tag2 == false)
  15. vec.push_back(root);
  16. tag1 = true;
  17. }
  18. else if (root == q) {
  19. if (tag2 == false && tag1 == false)
  20. vec.push_back(root);
  21. tag2 = true;
  22. }
  23. if (!tag1 && !tag2)
  24. vec.push_back(root);
  25. if (tag1 && tag2 && find(vec.begin(), vec.end(), root) != vec.end())
  26. return root;
  27.  
  28. if (lastRoot != root->right)
  29. {
  30. if (lastRoot != root->left) {
  31. if (root->left != nullptr) {
  32. sta.push(root->left);
  33. continue;
  34. }
  35. }
  36. if (root->right != nullptr) {
  37. sta.push(root->right);
  38. continue;
  39. }
  40. }
  41. lastRoot = root;
  42. sta.pop();
  43. }
  44. return nullptr;
  45. }

递归实现:

  1. TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
  2. if (!root || root == p || root == q) return root;
  3. TreeNode* l = lowestCommonAncestor(root->left, p, q);
  4. TreeNode* r = lowestCommonAncestor(root->right, p, q);
  5. return l && r ? root : l ? l : r;
  6. }

Lowest Common Ancestor of a Binary Tree leetcode的更多相关文章

  1. Lowest Common Ancestor of a Binary Tree——Leetcode

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

  2. Leetcode之236. Lowest Common Ancestor of a Binary Tree Medium

    236. Lowest Common Ancestor of a Binary Tree Medium https://leetcode.com/problems/lowest-common-ance ...

  3. leetcode 235. Lowest Common Ancestor of a Binary Search Tree 236. Lowest Common Ancestor of a Binary Tree

    https://www.cnblogs.com/grandyang/p/4641968.html http://www.cnblogs.com/grandyang/p/4640572.html 利用二 ...

  4. 【LeetCode】236. Lowest Common Ancestor of a Binary Tree

    Lowest Common Ancestor of a Binary Tree Given a binary tree, find the lowest common ancestor (LCA) o ...

  5. 【刷题-LeetCode】236. Lowest Common Ancestor of a Binary Tree

    Lowest Common Ancestor of a Binary Tree Given a binary tree, find the lowest common ancestor (LCA) o ...

  6. 88 Lowest Common Ancestor of a Binary Tree

    原题网址:https://www.lintcode.com/problem/lowest-common-ancestor-of-a-binary-tree/description 描述 给定一棵二叉树 ...

  7. [LeetCode] 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 ...

  8. LeetCode Lowest Common Ancestor of a Binary Tree

    原题链接在这里:https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/ 题目: Given a binary tr ...

  9. [LeetCode] 236. 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 ...

随机推荐

  1. 第6组UI组件:ViewAnimator及其子类

    ViewAnimator是一个基类,它继承了FrameLayout,因此它表现出FrameLayout的特征,可以将多个View组件“叠”在一起.ViewAnimator额外增加的功能正如它的名字所暗 ...

  2. 记一次DG搭建过程中备库ORA-00210,ORA-00202,ORA-27086错误

    ORA-00210: cannot open the specified control file ORA-00202: control file: '/u01/app/oracle/oradata/ ...

  3. 在ubuntu下增加root用户并登录

    1.打开终端. 2.输入sudo gedit /usr/share/lightdm/lightdm.conf.d/50-ubuntu.conf 3.在弹出的编辑框里输入:greeter-show-ma ...

  4. Word常用实用知识2

      纯手打,可能有错别字,使用的版本是office Word 2013  转载请注明出处 http://www.cnblogs.com/hnnydxgjj/p/6296863.html,谢谢. 批注和 ...

  5. 开源第三方登录组件OAuthLogin2.0 解析及开源地址

    OAuthLogin2.0介绍地址: 博客地址:http://www.cnblogs.com/dazhuangtage/p/6306133.html Nuget地址:https://www.nuget ...

  6. Ionic在windows下的环境配置难题

    具体的配置方法网上有很多,可以参考http://my.oschina.net/JeeChou/blog/219699?fromerr=k1hPtBUs,但问题还是会有,我总结的主要有以下几个方面: 1 ...

  7. BZOJ-2768: [JLOI2010]冠军调查(超级裸的最小割)

    2768: [JLOI2010]冠军调查 Time Limit: 10 Sec  Memory Limit: 128 MB Description 一年一度的欧洲足球冠军联赛已经进入了淘汰赛阶段.随着 ...

  8. 使用 visualstudio code 编辑器调试执行在 homestead 环境中的 laravel 程序

    由于之前做 .net 开发比较熟悉 visualstudio,所以自 visualstudio code 发布后就一直在不同场合使用 vscode ,比如前端.node等等.最近在做 laravel ...

  9. 《JAVASCRIPT高级程序设计》节点层次和DOM操作技术

    DOM可以将任何HTML和XML文档描绘成一个由多层次节点构成的结构.节点分为几种不同的类型,每种类型分别表示文档中不同的信息,每种类型都继承与Node接口,因此都共同享有一些属性和方法,同时,也拥有 ...

  10. maven常用命令介绍(持续更新)

    一.Maven的基本概念 主要服务于基于Java平台的项目构建,依赖管理和项目信息管理. 1.1.项目构建 项目构建过程包括[清理项目]→[编译项目]→[测试项目]→[生成测试报告]→[打包项目]→[ ...