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 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.
Subscribe to see which companies asked this question
非递归实现:
- TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
- if (root == nullptr)
- return root;
- stack<TreeNode*> sta;
- vector<TreeNode*> vec;
- bool tag1 = false;
- bool tag2 = false;
- sta.push(root);
- TreeNode* lastRoot = root;
- while (!sta.empty())
- {
- root = sta.top();
- if (root == p) {
- if(tag1 == false && tag2 == false)
- vec.push_back(root);
- tag1 = true;
- }
- else if (root == q) {
- if (tag2 == false && tag1 == false)
- vec.push_back(root);
- tag2 = true;
- }
- if (!tag1 && !tag2)
- vec.push_back(root);
- if (tag1 && tag2 && find(vec.begin(), vec.end(), root) != vec.end())
- return root;
- if (lastRoot != root->right)
- {
- if (lastRoot != root->left) {
- if (root->left != nullptr) {
- sta.push(root->left);
- continue;
- }
- }
- if (root->right != nullptr) {
- sta.push(root->right);
- continue;
- }
- }
- lastRoot = root;
- sta.pop();
- }
- return nullptr;
- }
递归实现:
- TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
- if (!root || root == p || root == q) return root;
- TreeNode* l = lowestCommonAncestor(root->left, p, q);
- TreeNode* r = lowestCommonAncestor(root->right, p, q);
- return l && r ? root : l ? l : r;
- }
Lowest Common Ancestor of a Binary Tree leetcode的更多相关文章
- 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 ...
- 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 ...
- 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 利用二 ...
- 【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 ...
- 【刷题-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 ...
- 88 Lowest Common Ancestor of a Binary Tree
原题网址:https://www.lintcode.com/problem/lowest-common-ancestor-of-a-binary-tree/description 描述 给定一棵二叉树 ...
- [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 ...
- LeetCode Lowest Common Ancestor of a Binary Tree
原题链接在这里:https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/ 题目: Given a binary tr ...
- [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 ...
随机推荐
- 第6组UI组件:ViewAnimator及其子类
ViewAnimator是一个基类,它继承了FrameLayout,因此它表现出FrameLayout的特征,可以将多个View组件“叠”在一起.ViewAnimator额外增加的功能正如它的名字所暗 ...
- 记一次DG搭建过程中备库ORA-00210,ORA-00202,ORA-27086错误
ORA-00210: cannot open the specified control file ORA-00202: control file: '/u01/app/oracle/oradata/ ...
- 在ubuntu下增加root用户并登录
1.打开终端. 2.输入sudo gedit /usr/share/lightdm/lightdm.conf.d/50-ubuntu.conf 3.在弹出的编辑框里输入:greeter-show-ma ...
- Word常用实用知识2
纯手打,可能有错别字,使用的版本是office Word 2013 转载请注明出处 http://www.cnblogs.com/hnnydxgjj/p/6296863.html,谢谢. 批注和 ...
- 开源第三方登录组件OAuthLogin2.0 解析及开源地址
OAuthLogin2.0介绍地址: 博客地址:http://www.cnblogs.com/dazhuangtage/p/6306133.html Nuget地址:https://www.nuget ...
- Ionic在windows下的环境配置难题
具体的配置方法网上有很多,可以参考http://my.oschina.net/JeeChou/blog/219699?fromerr=k1hPtBUs,但问题还是会有,我总结的主要有以下几个方面: 1 ...
- BZOJ-2768: [JLOI2010]冠军调查(超级裸的最小割)
2768: [JLOI2010]冠军调查 Time Limit: 10 Sec Memory Limit: 128 MB Description 一年一度的欧洲足球冠军联赛已经进入了淘汰赛阶段.随着 ...
- 使用 visualstudio code 编辑器调试执行在 homestead 环境中的 laravel 程序
由于之前做 .net 开发比较熟悉 visualstudio,所以自 visualstudio code 发布后就一直在不同场合使用 vscode ,比如前端.node等等.最近在做 laravel ...
- 《JAVASCRIPT高级程序设计》节点层次和DOM操作技术
DOM可以将任何HTML和XML文档描绘成一个由多层次节点构成的结构.节点分为几种不同的类型,每种类型分别表示文档中不同的信息,每种类型都继承与Node接口,因此都共同享有一些属性和方法,同时,也拥有 ...
- maven常用命令介绍(持续更新)
一.Maven的基本概念 主要服务于基于Java平台的项目构建,依赖管理和项目信息管理. 1.1.项目构建 项目构建过程包括[清理项目]→[编译项目]→[测试项目]→[生成测试报告]→[打包项目]→[ ...