Cracking-- 4.7 在一颗二叉树中找两个节点的第一个共同祖先
分别记录从 root 到 node1 的path 到 node2的path,再找其中共同的部分
struct Node{
int val;
struct Node *left;
struct Node *right;
Node(int _val)
{
val = _val;
left = NULL;
right = NULL;
}
}; bool getABPath(Node* root,vector<Node*> ansPiece, vector<Node*>& ansPiece1, vector<Node*>& ansPiece2, Node *a, Node *b)
{
if(root == NULL)
return false;
if(root == a)
{
ansPiece1 = ansPiece;
ansPiece1.push_back(root);
}
if(root == b)
{
ansPiece2 = ansPiece;
ansPiece2.push_back(root);
}
if(ansPiece1.empty() == false && ansPiece2.empty() == false)
{
return true;
} ansPiece.push_back(root);
bool ret1 = false, ret2 = false;
ret1 = getABPath(root->left,ansPiece,ansPiece1,ansPiece2,a,b); if(!ret1)
ret2 = getABPath(root->right,ansPiece,ansPiece1,ansPiece2,a,b);
if(ret2)
return true;
ansPiece.pop_back();
return false;
} Node* getCommonParent(Node *root, Node *a, Node *b)
{
if(root == NULL || a == NULL || b == NULL)
return root;
if(a == b)
return a; vector<Node*> ansPiece1;
vector<Node*> ansPiece2;
vector<Node*> ansPiece;
getABPath(root, ansPiece, ansPiece1, ansPiece2, a, b); // doesn't exist in the tree
if(ansPiece1.size() == || ansPiece2.size() == )
return NULL;
int i;
for(i = ; i < ansPiece1.size() && i < ansPiece2.size(); i++)
{
if(ansPiece1[i] != ansPiece2[i])
break;
} return ansPiece1[i - ];
}
Cracking-- 4.7 在一颗二叉树中找两个节点的第一个共同祖先的更多相关文章
- [程序员代码面试指南]二叉树问题-在二叉树中找到两个节点的最近公共祖先、[LeetCode]235. 二叉搜索树的最近公共祖先(BST)(非递归)
题目 题解 法一: 按照递归的思维去想: 递归终止条件 递归 返回值 1 如果p.q都不在root为根节点的子树中,返回null 2 如果p.q其中之一在root为根节点的子树中,返回该节点 3 如果 ...
- Leetcode 671.二叉树中第二小的节点
二叉树中第二小的节点 给定一个非空特殊的二叉树,每个节点都是正数,并且每个节点的子节点数量只能为 2 或 0.如果一个节点有两个子节点的话,那么这个节点的值不大于它的子节点的值. 给出这样的一个二叉树 ...
- 六:二叉树中第k层节点个数与二叉树叶子节点个数
二叉树中第k层节点个数 递归解法: (1)假设二叉树为空或者k<1返回0 (2)假设二叉树不为空而且k==1.返回1 (3)假设二叉树不为空且k>1,返回左子树中k-1层的节点个数与右子树 ...
- LeetCode 671. 二叉树中第二小的节点(Second Minimum Node In a Binary Tree) 9
671. 二叉树中第二小的节点 671. Second Minimum Node In a Binary Tree 题目描述 给定一个非空特殊的二叉树,每个节点都是正数,并且每个节点的子节点数量只能为 ...
- python3实现在二叉树中找出和为某一值的所有路径
在二叉树中找出和为某一值的所有路径请写一个程序创建一棵二叉树,并按照一定规则,输出二叉树根节点到叶子节点的路径.规则如下:1.从最顶端的根结点,到最下面的叶子节点,计算路径通过的所有节点的和,如果与设 ...
- Java实现 LeetCode 671 二叉树中第二小的节点(遍历树)
671. 二叉树中第二小的节点 给定一个非空特殊的二叉树,每个节点都是正数,并且每个节点的子节点数量只能为 2 或 0.如果一个节点有两个子节点的话,那么这个节点的值不大于它的子节点的值. 给出这样的 ...
- [Swift]LeetCode671. 二叉树中第二小的节点 | Second Minimum Node In a Binary Tree
Given a non-empty special binary tree consisting of nodes with the non-negative value, where each no ...
- LeetCode 671. Second Minimum Node In a Binary Tree二叉树中第二小的节点 (C++)
题目: Given a non-empty special binary tree consisting of nodes with the non-negative value, where eac ...
- [LeetCode] 671. 二叉树中第二小的节点 ☆(递归 合并)
描述 给定一个非空特殊的二叉树,每个节点都是正数,并且每个节点的子节点数量只能为 2 或 0.如果一个节点有两个子节点的话,那么这个节点的值不大于它的子节点的值. 给出这样的一个二叉树,你需要输出所有 ...
随机推荐
- asp.net(C#)利用QRCode生成二维码(续)-在二维码图片中心加Logo或图像
<%@ WebHandler Language="C#" Class="GetQRCode" %> using System; using Syst ...
- mybatis, spring, springmvc
mybatis配置: mybatis-config.xml <configuration> <!-- 作者MyBatis博客: http://legend2011.blog.51ct ...
- LinQ高级查询
1.模糊查询 con.Users.Where(a =>a.UserName.Contains(name)).ToList(); //包含name con.Users.Where(a =>a ...
- linux grep -I 属性
忽略大小写的查找: grep -i 'address' test.log --> address ADDRESS
- c++的默认构造函数 VS 深拷贝(值拷贝) 与 浅拷贝(位拷贝)
C++默认为类生成了四个缺省函数: A(void); // 缺省的无参数构造函数 A(const A &a); // 缺省的拷贝构造函数 ~A(void); // 缺省的析构函数 A & ...
- h5 摄像头处理 在线视频
http://www.360doc.com/content/08/0812/03/72059_1533104.shtml http://html5online.com.cn/articles/2012 ...
- WIN7环境下CUDA7.5的安装、配置和测试(Visual Studio 2010)
以下基于"WIN7(64位)+Visual Studio 2010+CUDA7.5". 系统:WIN7,64位 开发平台:Visual Studio 2010 显卡:NVIDIA ...
- django单元测试
django 单元测试小结 django 测试 从前很少写单元测试了,特别是web应用.最近不知不觉喜欢起来这个事情了,发现单元测试对于软件的模块,正交性有很大促进作用,因为函数,模块写的不合 ...
- 关于一个程序的编译过程 zkjg面试
http://blog.csdn.net/gengyichao/article/details/6544266 一 以下是C程序一般的编译过程: 从图中看到: 将编写的一个c程序(源代码 )转换成可以 ...
- C#获取程序集自动增加的版本号和编译时间
1. 首先找到文件AssemblyInfo.cs, 路径如下: 2. 修改版本的格式,修改后,程序每次编译,程序集的版本号都会自增. 修改前: [assembly: AssemblyVersion(& ...