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)的更多相关文章

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

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

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

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

  5. 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 利用二 ...

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

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

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

  9. (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 ...

随机推荐

  1. Unity3D开发之查找面板上某个脚本(包括Missing)

    原地址:http://blog.csdn.net/lihandsome/article/details/24265411 有时候我们需要知道某个脚本在场景上面哪里用到,或者那个脚本被删除了但又没有把相 ...

  2. 李洪强漫谈iOS开发[C语言-013]-常量

    // //  main.m //  09 - 常量 // //  Created by 李洪强 on 16/7/17. //  Copyright © 2016年 李洪强. All rights re ...

  3. C++ 通过对象方式 、指针方式两种方式去访问成员变量(属性或者方法)

    准备 1.在VS中新建一个项目-Viscal C++ ---常规--空项目 2.建立一个.h的头文件 定义一个类 声明其成员(C#中的属性和方法) #include<iostream> # ...

  4. java:比较对象

    对象内容相等条件:1.对象类型相同(可用instanceof操作符比较)2.对象的成员变量的值完全相同 instanceof 判断对象类型 //a是否为Child对象类型 boolean b = a ...

  5. 初始化windows窗口

    LRESULT WINAPI WndProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam) { PAINTSTRUCT ps; switch (msg ...

  6. Ubuntu 学习笔记

    1.   ubuntu开启root账号,设置分配很简单,只要为root设置一个root密码就行了: $ sudo passwd root 之后会提示要输入root用户的密码,连续输入root密码,再使 ...

  7. C#中配置文件的使用

    1. 向项目添加app.config文件: 右击项目名称,选择“添加”→“添加新建项”,在出现的“添加新项”对话框中,选择“添加应用程序配置文件”:如果项目以前没有配置文件,则默认的文件名称为“app ...

  8. Win7安装错误提示与解决办法大全

    Windows7安装时有许多提示错误,许多朋友不知道如何解决,那就看看这篇软媒整理的文章吧,或许有些帮助.本文出现的问题同样应用于其他版本的Windows 7,甚至是Vista,收藏一下本文,或者某天 ...

  9. 使用Zxing实现扫二维码描

    1.集成Zxing.bar 2.复制代码到项目中 3.修改 MipacActivityCapture.java  的扫描结果方法 handleDecode() /** * 处理扫描结果,实现活动页面跳 ...

  10. Flask

    #environ:一个包含所有HTTP请求信息的dict对象 #start_response:一个发送HTTP响应的函数 def application(environ, start_response ...