[Twitter] Lowest Level Common Ancestor

Question:

In a binary integer value tree, find the lowest level common ancestor of two values.

http://www.glassdoor.com/Interview/In-a-binary-integer-value-tree-find-the-lowest-level-common-ancestor-of-two-values-QTN_219955.htm

// class TreeNode{
//   int val;
//   TreeNode left; 
//   TreeNode right;
//  }
//
// Lowest common ancestor question
//
// If it is a BST tree
// Given root, if two inputs are both smaller than root, the common ancestor must be on the root left.
// If two inputs are both bigger than root, the common ancestor must be on the right
// otherwise, the root is the common ancestor.
public TreeNode findLowestCommonAncestor(TreeNode root, TreeNode a, TreeNode b)
{
    // Input validations
    // root cannot be null.
    // a || b cannot be null.
    // a and b must be node in the tree.
    
    if (a.val < root.val && b.val < root.val)
        return findLowestCommonAncestor(root.left, a, b);
    else if (a.val > root.val && b.val > root.val)
        return findLowestCommonAncestor(root.right, a, b);
    else
        return root;
}

// For BT
// We can Buttom-Up
// O(n)
public TreeNode findLowestCommonAncestor(TreeNode root, TreeNode a, TreeNode b)
{
    if (root == null)
        return null;

    if (root == a || root == b)
        return root;
        
    TreeNode findLeft = findLowestCommonAncestor(root.left, a, b);
    TreeNode findRight = findLowestCommonAncestor(root.right, a, b);
    if (findLeft != null && findRight != null)
        return root;
    else if (findLeft != null)
        return findLeft;
    else
        return findRight ;
}

// Or we can top down
//
// If it is just a normal BT
// Given a root
// Define hit(root, a, b):int
// as the number of nodes in root.
// If a and b are both in root, hit = 2
// if only a in root or only b in root, hit = 1
// otherwise hit = 0
//
// O(n^2) : for each node, iterate the whole tree.
public TreeNode findLowestCommonAncestor(TreeNode root, TreeNode a, TreeNode b)
{
  int lefthit = hit(root, a, b);
  if (lefthit == 1)
      return root;
  else if (lefthit == 2)
      return findLowestCommonAncestor(root.left, a, b);
  else
      return findLowestCommonAncestor(root.right, a, b);
}

// Assume a != b
private int hit(TreeNode node, TreeNode a, TreeNode b)
{
  if (node == null || a == null || b == null)
      return 0;
      
  int r = 0;
  if (node == a)
  {
      a = null;
      r ++;
  }
  if (node == b)
  {
      b = null;
      r ++;
  }
  
  return r + hit(node.left, a, b) + hit(node.right, a, b);
}
时间: 2024-08-02 02:50:26

[Twitter] Lowest Level Common Ancestor的相关文章

Lowest Common Ancestor in Binary Tree

The problem: Given node P and node Q in a binary tree T. Find out the lowest common ancestor of the two nodes. Analysis: The answer of this problem could only be the following two cases: case 1: P or Q itself is the lowest common ancestor. P ........

235. Lowest Common Ancestor of a Binary Search Tree

1. 问题描述 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 th

Lowest Common Ancestor of a Binary Search Tree

1. Title 235. Lowest Common Ancestor of a Binary Search Tree 2. Http address https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/ 3. The question Given a binary search tree (BST), find the lowest common ancestor (LCA) of two

Lowest Common Ancestor of a Binary Tree

题目连接 https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/ Common Ancestor of a Binary Tree Description Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According to the definition of LCA on

数据结构与算法(1)支线任务4——Lowest Common Ancestor of a Binary 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 def

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 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 OJ: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

leetcode - Lowest Common Ancestor of a Binary Search Tree

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 BST. According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined

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 tree. Avoid storing additional nodes in a data structure. NOTE: This is not necessarily a binary search tree. LeetCode上的原题,请参见我之前的博客Lowest Common Ancest