分别记录从 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. CSS3常用选择器(三)

    在CSS3的选择器中,除了结构性伪类选择器外,还有一种UI元素状态伪类选择器.这些选择器的共同特征: 指定的样式只有当元素处于某种状态时才起作用,在默认状态下不起作用. 1.hover.focus.a ...

  2. invoke

    在用.NET Framework框架的WinForm构建GUI程序界面时,如果要在控件的事件响应函数中改变控件的状态,例如:某个按钮上的文本原先叫“打开”,单击之后按钮上的文本显示“关闭”,初学者往往 ...

  3. UML大战需求分析阅读笔记1

    UML这三个字母的全称是Unified Modeling Language,直接翻译就是统一建模语言,简单地说就是一种有特殊用途的语言.你可能会问:这明明是一种图形,为什么说是语言呢?伟大的汉字还不是 ...

  4. Myeclipse2016部署tomcat服务(别的服务类似)配置环境

    1.在工具MyEclipse的项目管理菜单中,右单机找Properties或者快捷键alt+enter,(或者直接搜索Runtimes)myEclipse/Targeted Runtimes 2.ne ...

  5. 使用新浪云 Java 环境搭建一个简单的微信处理后台

    前一段时间,写了一篇在新浪云上搭建自己的网站的教程,通过简单构建了一个 maven 的项目,展示部署的整个流程,具体的操作可以参看这里. 新浪云服务器除了可以搭建自己的网站以外,也非常的适合作为微信公 ...

  6. Shell学习笔记 - 环境变量配置文件(转)

    一.source命令 功能:在当前bash环境下读取并执行配置文件中的命令 1. 命令格式 source 配置文件  或  . 配置文件 2. 命令示例 [root@localhost ~]# sou ...

  7. Light OJ 1029- Civil and Evil Engineer (图论-最小生成树)

    题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1029 题目大意:一个发电站,给n座房子供电, 任意房子之间有电线直接或者间接相 ...

  8. js-url打开方式

    引用自 : 老张的博客 *.location.href 用法: top.location.href="url"          在顶层页面打开url(跳出框架) self.loc ...

  9. java获取classpath

    public class PathTest { public static void main(String[] args) { //获取根路径三种方式 System.out.println(Path ...

  10. hibernate报错Unknown integral data type for ids : java.lang.String

    package com.model; // Generated 2016-10-27 14:02:17 by Hibernate Tools 4.3.1.Final /** * CmDept gene ...