分别记录从 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. Spark 2.6.1 源代码在 eclipse 的配置

    本文地址:http://www.cnblogs.com/jying/p/3671767.html 这么个问题又耗费了偶一天时间,真是羞愧.. 上午从官网svn地址下载最新的 spark 包,总是下载失 ...

  2. shell 里的变量 总结

    对于linux shell的使用者来说, 巧妙的应用变量不仅能够快速的解决问题,同时能够获取非常大的乐趣,因为shell的变量内部可以附加一些运算,使得程序非常简洁明了并且功能强大,以下详细介绍一下: ...

  3. Masonry学习笔记

    1.边距 [bottomView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self.view).offs ...

  4. Java中的值传递和引用传递

    这几天一直再纠结这个问题,今天看了这篇文章有点思路了,这跟C++里函数参数为引用.指针还是有很大区别. 当一个对象被当作参数传递到一个方法后,此方法可改变这个对象的属性,并可返回变化后的结果,那么这里 ...

  5. matlab的try/catch语句

    http://blog.sina.com.cn/s/blog_6fd1f2350102x2p3.html

  6. 史航416第八次作业&总结

    一.知识点总结: 1.数组的输入,输出及对整个数组所有元素进行操作通常都用循环结构实现. 2.可以只给部分元素赋初值.当{ }中值的个数少于元素个数时,只给前面部分元素赋值. 3.只能给元素逐个赋值, ...

  7. Linux QT

    Check system version 1.  cat /proc/version mike@sp-ThinkPad-X220:~$ cat /proc/versionLinux version 3 ...

  8. switch语句下的变量声明和定义

    switch语句下的变量声明和定义的问题: switch...case...语句中存在声明和定义会出现一些问题.这个由switch语法特性决定的, switch中每个case都是平等的层次,区别于一般 ...

  9. position:absolute绝对定位解读

    position:absolute绝对定位解读  摘要   用四段代码解释absolute的定位问题,进而从概念的角度切实解决html布局问题. 一.背景 常常遇到这样一些问题,很容易混淆.“浏览器屏 ...

  10. 反汇编工具capstone安装后import error

    使用sudo pip install capstone后,使用如下代码import时出现error. from capstone import * 错误信息: File "/usr/loca ...