leetcode@ [236] Lowest Common Ancestor of a Binary Tree(Tree)
https://leetcode.com/problems/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 to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes v and w as the lowest node in T that has both v and w as descendants (where we allow a node to be a descendant of itself).”
For example, the lowest common ancestor (LCA) of nodes 5
and 1
is 3
. Another example is LCA of nodes 5
and 4
is 5
, since a node can be a descendant of itself according to the LCA definition.
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
void findPath(TreeNode* root, vector<TreeNode* >& load, vector<TreeNode* >& path, TreeNode* p) {
if(root == NULL) return;
if(root == p) {
path = load;
return;
} load.push_back(root->left);
findPath(root->left, load, path, p);
load.pop_back(); load.push_back(root->right);
findPath(root->right, load, path, p);
load.pop_back();
}
void reverse_vector(vector<TreeNode* > v) {
vector<TreeNode* > res; res.clear(); for(int i=v.size()-;i>=;--i) res.push_back(v[i]);
v = res;
}
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
if(p == q) return p; vector<TreeNode* > load1, load2, path1, path2; TreeNode *r = root;
load1.push_back(r);
load2.push_back(r); findPath(r, load1, path1, p);
findPath(r, load2, path2, q); reverse_vector(path1);
reverse_vector(path2); int i = , j = , m = path1.size(), n = path2.size(), resi;
cout << m << n << endl;
while(i < m && j < n && path1[i] == path2[j]) {
resi = i; ++i; ++j;
} return path1[resi];
}
};
leetcode: 236
leetcode@ [236] Lowest Common Ancestor of a Binary Tree(Tree)的更多相关文章
- LeetCode (236):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. According ...
- [LeetCode] 235. 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. According ...
- leetcode 235. Lowest Common Ancestor of a Binary Search Tree 236. Lowest Common Ancestor of a Binary Tree
https://www.cnblogs.com/grandyang/p/4641968.html http://www.cnblogs.com/grandyang/p/4640572.html 利用二 ...
- 235.236. Lowest Common Ancestor of a Binary (Search) Tree -- 最近公共祖先
235. Lowest Common Ancestor of a Binary Search Tree Given a binary search tree (BST), find the lowes ...
- [LeetCode] 235. 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. According ...
- (medium)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 ...
随机推荐
- uva 10892
试了一下纯暴力 结果过了 无话可说 应该有更好的方法...... /**************************************************************** ...
- Unique Binary Search Trees II
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...
- linux samba.tar.gz安装和配置
安装步骤: 1. tar -xzvf samba-3.5.10.tar.gz2. cd samba-3.5.103. cd source34. ./autogen.sh 如果出现:./autogen ...
- linux ubuntu删除引导 grub出现错误解决方案
使用u盘启动PE系统 找到diskgenius软件,点击: 硬盘->重建主引导记录
- [模拟]ZOJ3480 Duck Typing
题意:给了一坨...按题目意思输出就好了... 给一组案例 begin class d class c:d class b:c class a:b def d.m def d.n call a.m e ...
- 图形学:图像围绕着某个点P(a,b)旋转------白话版
前提:在研究图形时候,我们并没有规定图形的大小,所以任意图形多是支持的,这也另外说明了一点,图形转换和图形的大小没有关系. 如果图像围绕着某个点P(a,b)旋转,则先要将坐标系平移到该点,再进行旋转, ...
- 如何快速查看linux的发行版信息
思路一: 在CentOS中想查看发行版信息,输入了lsb_release -a 命令却报错了,通过输入以下命令进行安装 yum install redhat-lsb -y 然后继续查看发行版信息 [r ...
- nginx + tomcat
http://blog.csdn.net/sun305355024sun/article/details/8620996
- PHP 简单的加密解密算法
<?php /** * * @创建时间:2015-3-12 下午2:07:33 * @作者:YuMing * @描述:异或加密解密类 */ class Yihuo extends CI_Cont ...
- oracle时间加减的语句写法
FROM: http://soft.doit.com.cn/article/2012/0105/2850851.shtml --加法 --加1年 SELECT SYSDATE,ADD_MONTHS( ...