Lowest Common Ancestor of a Binary Tree, with Parent Pointer

Given a binary tree, find the lowest common ancestor of two given nodes in tree. Each node contains a parent pointer which links to its parent.

int getHeight(Node* p)
{
    int height = 0;
    while (p)
    {
        height++;
        p = p->parent;
    }
    return height;
}

Node* LCA(Node* p, Node *q)
{
    int h1 = getHeight(p);
    int h2 = getHeight(q);
    if (h1 > h2)
    {
        swap(h1, h2);
        swap(p, q);
    }
    for (int h = 0; h < h2-h1; h++)
    {
        q = q->parent;
    }
    while (p && q)
    {
        if (p == q)
            return p;
        p = p->parent;
        q = q->parent;
    }
    return NULL;
}
时间: 2024-10-19 10:52:49

Lowest Common Ancestor of a Binary Tree, with Parent Pointer的相关文章

[LeetCode][JavaScript]Lowest Common Ancestor of a Binary Tree

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 th

235. Lowest Common Ancestor of a Binary Search Tree &amp;&amp; 236. Lowest Common Ancestor of a Binary Tree

Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST. 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

leetcode_236——Lowest Common Ancestor of a Binary Tree(tree,后序遍历)

Lowest Common Ancestor of a Binary Tree Total Accepted: 4228 Total Submissions: 15884My Submissions Question Solution Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According to the definition of LCA on Wik

Lowest Common Ancestor of a Binary Tree题解

这是LeetCode上的一道题,让我们先来看一看题目: 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 nod

235 Lowest Common Ancestor of a Binary Tree

题目 235 Lowest Common Ancestor of a Binary Tree 因为是binary search tree,因此利用没个节点的值进行二分查找即可复杂度O(h) class Solution: # @param {TreeNode} root # @param {TreeNode} p # @param {TreeNode} q # @return {TreeNode} def lowestCommonAncestor(self, root, p, q): if p.

Lowest Common Ancestor of a Binary Tree解析

Lowest Common Ancestor of a Binary Tree Total Accepted: 6162 Total Submissions: 23311 My Submissions Question Solution Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According to the definition of LCA on Wi

【LeetCode】236. Lowest Common Ancestor of a Binary Tree

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 th

88 Lowest Common Ancestor of a Binary Tree

原题网址:https://www.lintcode.com/problem/lowest-common-ancestor-of-a-binary-tree/description 描述 给定一棵二叉树,找到两个节点的最近公共父节点(LCA). 最近公共祖先是两个节点的公共的祖先节点且具有最大深度. 假设给出的两个节点都在树中存在 您在真实的面试中是否遇到过这个题?  是 样例 对于下面这棵二叉树 4 / 3 7 / 5 6 LCA(3, 5) = 4 LCA(5, 6) = 7 LCA(6, 7

LeetCode 236. Lowest Common Ancestor of a Binary Tree; 235. Lowest Common Ancestor of a Binary Search Tree

236. Lowest Common Ancestor of a Binary Tree 递归寻找p或q,如果找到,层层向上返回,知道 root 左边和右边都不为NULL:if (left!=NULL && right!=NULL) return root; 时间复杂度 O(n),空间复杂度 O(H) class Solution { public: TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode*