博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Lowest Common Ancestor of a Binary Tree
阅读量:5036 次
发布时间:2019-06-12

本文共 3605 字,大约阅读时间需要 12 分钟。

Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.

According to the : “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).”

_______3______       /              \    ___5__          ___1__   /      \        /      \   6      _2       0       8         /  \         7   4

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.

 

hint:利用先序遍历分别获取root节点到目标节点的路径,然后比较两路径最长公布部分,就可以发现他们的LCA

/** * 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:    TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {        vector
p_path, q_path; vector
path; get_path(root, p, &path, &p_path); // 获取root到p的节点路径 path.clear(); get_path(root, q, &path, &q_path); // 获取root到q的节点路径 int len = min(p_path.size(), q_path.size()); int i = 0; while (i < len && p_path[i] == q_path[i]) { // 找到第一个不同的节点,那么前一个节点就是LCD ++i; } return p_path[i-1]; } void get_path(TreeNode* root, TreeNode* target, vector
* path, vector
* target_path) { if (!target_path->empty() && target_path->back() == target) { // 如果已经找到了root到target节点的路径,那就直接不用搜 return ; } if (root == NULL) { // 空姐点直接不搜 return ; } path->push_back(root); // 访问根节点,加入路径 if (path->back() == target) { // 判断是否是到target节点 *target_path = *path; } get_path(root->left, target, path, target_path); // 继续深搜左子树 get_path(root->right, target, path, target_path); // 继续深搜右子树 path->pop_back(); // 回溯 }};

 

原来还有一道BST的LCA

Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.

According to the : “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).”

_______6______       /              \    ___2__          ___8__   /      \        /      \   0      _4       7       9         /  \         3   5

For example, the lowest common ancestor (LCA) of nodes 2 and 8 is 6. Another example is LCA of nodes 2 and 4 is 2, since a node can be a descendant of itself according to the LCA definition.

 

 

 hint:LCA->val 在 p->val与q->val之间
/** * 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:    TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {        int max_val = p->val, min_val = q->val;        if (max_val < min_val) {            swap(max_val, min_val);        }                TreeNode* cur = root;        while (cur != NULL) {            if (cur->val < min_val) {   // 若当前节点比两目标节点的值都小,需右移                cur = cur->right;            } else if (cur->val > max_val) {    // 若当前节点比两目标节点的值都大,需左移                cur = cur->left;            } else {                break;            }        }                return cur;    }};

 

转载于:https://www.cnblogs.com/bugfly/p/5205954.html

你可能感兴趣的文章
[置顶] 细说Cookies
查看>>
[wp7软件]wp7~~新闻资讯,阅读软件下载大全! 集合贴~~~
查看>>
生成指定位数随机数的方法
查看>>
Essential C++学习笔记
查看>>
where,having与 group by连用的区别
查看>>
【MySQL】MySQL锁和隔离级别浅析二 之 INSERT
查看>>
Oracle T4-2 使用ILOM CLI升级Firmware
查看>>
4.14上午
查看>>
数据分析 -- 白话一下什么是决策树模型(转载)
查看>>
Java SPI机制原理和使用场景
查看>>
web前端java script学习2017.7.18
查看>>
删除TXPlatform
查看>>
LaTex:图片排版
查看>>
并发访问超时的问题可能性(引用)
查看>>
中小团队基于Docker的Devops实践
查看>>
利用python打开摄像头并保存
查看>>
System函数的使用说明
查看>>
Selenium-测试对象操作之:获取浏览器滚动条滚动距离
查看>>
Linux下MySQL数据库安装与配置
查看>>
Extjs String转Json
查看>>