48. 二叉树两结点的最低共同父结点(3种变种情况)[Get lowest common ancestor of binary tree]
【题目】
输入二叉树中的两个结点,输出这两个结点在数中最低的共同父结点。
二叉树的结点定义如下:
|
1
2 3 4 5 6 |
struct BinaryTreeNode
{ int value; BinaryTreeNode *left; BinaryTreeNode *right; }; |
【分析】
求数中两个结点的最低共同结点是面试中经常出现的一个问题。这个问题有几个变种。
【变种1】
第一个变种是二叉树是一种特殊的二叉树:查找二叉树。也就是树是排序过的,位于左子树上的结点都比父结点小,而位于右子树的结点都比父结点大。我们只需要从根结点开始和两个结点进行比较。如果当前结点的值比两个结点都大,则最低的共同父结点一定在当前结点的左子树中。如果当前结点的值比两个结点都小,则最低的共同父结点一定在当前结点的右子树中。
具体代码如下:
|
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
// 48_GetLowestCommonAncessor.cpp : Defines the entry point for the console application.
// #include "stdafx.h" struct BinaryTreeNode BinaryTreeNode* create_tree_r(int a[],int left,int right) BinaryTreeNode* CreateTree(int a[],int length) // whether tree root has node x? // check whether root tree has node // check in left right-tree // get lowest common ancestor recursively // get lowest common ancestor iteratively // get lca for x and y void test_base(BinaryTreeNode *root,int x,int y) void test_case() int _tmain(int argc, _TCHAR* argv[]) |
【变种2】
第二个变种是树为多叉树,每个结点都有一个指针指向它的父结点。于是我们可以从任何一个结点出发,得到一个到达树根结点的单向链表。因此这个问题转换为两个单向链表的第一个公共结点,之前35.两链表的第一个公共结点已经讨论过。
【变种3】
第三个变种是树为多叉树,每个父节点有若干个子节点,但是子节点没有指向父节点指针。我们只能从根节点遍历树,从而得到从根节点到某一节点的路径,然后求这两个路径的最后一个公共节点。
本题中的二叉树是第三个变种的一个特例,即每个父节点只有左右子节点。
具体代码如下:
|
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
#include <vector>
#include <iostream> using namespace std; // treenode // get tree node path from root to node // get last common node of path1 and path2 TreeNode *lastNode = NULL; // get lowest common ancestor |
【参考】
http://zhedahht.blog.163.com/blog/static/25411174201081263815813/
http://blog.csdn.net/dahai_881222/article/details/7801356
http://www.cnblogs.com/venow/archive/2012/08/31/2664969.html
【本文链接】
http://www.cnblogs.com/hellogiser/p/get-lowest-common-ancestor-of-binary-tree.html
48. 二叉树两结点的最低共同父结点(3种变种情况)[Get lowest common ancestor of binary tree]的更多相关文章
- Lowest Common Ancestor of a Binary Search Tree(树中两个结点的最低公共祖先)
题目描述: Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in ...
- [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] 1123. Lowest Common Ancestor of Deepest Leaves 最深叶结点的最小公共父节点
Given a rooted binary tree, return the lowest common ancestor of its deepest leaves. Recall that: Th ...
- [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 ...
- [CareerCup] 4.7 Lowest Common Ancestor of a Binary Search Tree 二叉树的最小共同父节点
4.7 Design an algorithm and write code to find the first common ancestor of two nodes in a binary tr ...
- [Swift]LeetCode236. 二叉树的最近公共祖先 | 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 ...
- 51.Lowest Common Ancestor of a Binary Tree(二叉树的最小公共祖先)
Level: Medium 题目描述: Given a binary tree, find the lowest common ancestor (LCA) of two given nodes ...
- [LeetCode] Lowest Common Ancestor of a Binary Search Tree 二叉搜索树的最小共同父节点
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...
- [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. Accordi ...
随机推荐
- 【WCF】wcf不支持的返回类型
亲测不支持 DateView 不支持函数重载 参看:http://www.cnblogs.com/zeroone
- webBrowser1
HTMLDocument类的引用 using mshtml;
- JavaScript函数表达式、闭包、模仿块级作用域、私有变量
函数表达式是一种非常有用的技术,使用函数表达式可以无需对函数命名,从而实现动态编程.匿名函数,是一种强大的方式,一下总结了函数表达式的特点: 1.函数表达式不同于函数声明,函数声明要求有名字,但函数表 ...
- [webpack] 配置react+es6开发环境
写在前面 每次开新项目都要重新安装需要的包,简单记录一下. 以下仅包含最简单的功能: 编译react 编译es6 打包src中入口文件index.js至dist webpack配置react+es6开 ...
- Mybatis传入参数类型为Map
mybatis更新sql语句: <update id="publishT00_notice" parameterType="Map"> update ...
- Mysql 如何批量插入百万行测试数据
Mysql 如何批量插入百万行测试数据
- js修改伪类的值
css文件 p.change:after { content: attr(data-content); } js文件 $(this).addClass('change').attr('data-con ...
- 谈iOS抓包:Mac下好用的HTTP/HTTPS抓包工具Charles
在Mac下做开发,用Fiddler抓包由于离不开Windows比较痛苦,还好有Charles,到官网http://www.charlesproxy.com/可下载到最新版本(若不支持rMBP可拖到Re ...
- RP
关键印象 Base note 任务(task):一段代码. 进程(process):程序的实体,一个正在运行中的可执行文件.拥有独立的虚拟内存空间和系统资源,包括端口权限. 线程(thread):进程 ...
- 在thinkphp中js文件添加路径
JS文件不认识__URL__这些tp独特的链接,所以要自己重新定义url,才能使用这些路径. 在js对于的模板上定义url 然后在js文件中引用