分别记录从 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 在一颗二叉树中找两个节点的第一个共同祖先的更多相关文章

  1. [程序员代码面试指南]二叉树问题-在二叉树中找到两个节点的最近公共祖先、[LeetCode]235. 二叉搜索树的最近公共祖先(BST)(非递归)

    题目 题解 法一: 按照递归的思维去想: 递归终止条件 递归 返回值 1 如果p.q都不在root为根节点的子树中,返回null 2 如果p.q其中之一在root为根节点的子树中,返回该节点 3 如果 ...

  2. Leetcode 671.二叉树中第二小的节点

    二叉树中第二小的节点 给定一个非空特殊的二叉树,每个节点都是正数,并且每个节点的子节点数量只能为 2 或 0.如果一个节点有两个子节点的话,那么这个节点的值不大于它的子节点的值. 给出这样的一个二叉树 ...

  3. 六:二叉树中第k层节点个数与二叉树叶子节点个数

    二叉树中第k层节点个数 递归解法: (1)假设二叉树为空或者k<1返回0 (2)假设二叉树不为空而且k==1.返回1 (3)假设二叉树不为空且k>1,返回左子树中k-1层的节点个数与右子树 ...

  4. LeetCode 671. 二叉树中第二小的节点(Second Minimum Node In a Binary Tree) 9

    671. 二叉树中第二小的节点 671. Second Minimum Node In a Binary Tree 题目描述 给定一个非空特殊的二叉树,每个节点都是正数,并且每个节点的子节点数量只能为 ...

  5. python3实现在二叉树中找出和为某一值的所有路径

    在二叉树中找出和为某一值的所有路径请写一个程序创建一棵二叉树,并按照一定规则,输出二叉树根节点到叶子节点的路径.规则如下:1.从最顶端的根结点,到最下面的叶子节点,计算路径通过的所有节点的和,如果与设 ...

  6. Java实现 LeetCode 671 二叉树中第二小的节点(遍历树)

    671. 二叉树中第二小的节点 给定一个非空特殊的二叉树,每个节点都是正数,并且每个节点的子节点数量只能为 2 或 0.如果一个节点有两个子节点的话,那么这个节点的值不大于它的子节点的值. 给出这样的 ...

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

  8. 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 ...

  9. [LeetCode] 671. 二叉树中第二小的节点 ☆(递归 合并)

    描述 给定一个非空特殊的二叉树,每个节点都是正数,并且每个节点的子节点数量只能为 2 或 0.如果一个节点有两个子节点的话,那么这个节点的值不大于它的子节点的值. 给出这样的一个二叉树,你需要输出所有 ...

随机推荐

  1. 网络同步带来的bug

    说一下之前遇到的bug,首先贴点代码 public class TcpSession : ITcpSession { private Socket _appSession; private AutoR ...

  2. 项目在build machine中失败,本地Build成功的程序集版本问题

    MSBuild在build machine中遇到which has a higher version than its reference assembly:(in my case let's say ...

  3. python模块学习心得

    初始模块 1.什么是模块 模块是用来实现某项功能的一大堆代码,为什么会有模块呢?过程式编程的时候为了减少程序员编程代码的重复性,就利用函数的调用减少了代码的重复性,但是某些时候程序会过于的庞大,我们会 ...

  4. IUnknown(TVarData(Params[0]).VPointer) as Range

    IUnknown(TVarData(Params[0]).VPointer) as Range 修改为  IUnknown(TVarData(Params[0]).VPointer) as WOrd_ ...

  5. 第十八周个人作业--The Final

    项目计划   完成这个项目需要的时间:5-7天项目开发  需求分析:    作为一名排球赛事管理者,我希望能够根据比赛查询每场比赛的结果,以便于确定每支球队的比赛名次.  设计文档    由排球比赛用 ...

  6. 机器学习--Classifier comparison

    最近在学习机器学习,学习和积累和一些关于机器学习的算法,今天介绍一种机器学习里面各种分类算法的比较 #!/usr/bin/python # -*- coding: utf-8 -*- "&q ...

  7. spring 异常记录

    1.异常: java.lang.IllegalArgumentException: No converter found for return value of type: class java.ut ...

  8. jquery的滑动

    (1)slideDown(speed,callback)方法:用于想下滑动的方法. $("#flip").click(function(){ $("#panel" ...

  9. git 记住密码

    http://yourname:password@git.oschina.net/name/project.git

  10. OpenLDAP,一登录系统就修改密码

    http://guodayong.blog.51cto.com/263451/d-2 郭大勇的博客   1:修改配置文件 在前面打开注释 moduleload ppolicy.la modulepat ...