题目描述
题目难度:Medium
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 p and q as the lowest node in T that has both p and q as descendants (where we allow a node to be a descendant of itself).”
Given the following binary tree: root = [3,5,1,6,2,0,8,null,null,7,4]
- Example 1:
Input: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1
Output: 3
Explanation: The LCA of nodes 5 and 1 is 3.
- Example 2:
Input: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4
Output: 5
Explanation: The LCA of nodes 5 and 4 is 5, since a node can be a descendant of itself according to the LCA definition.
Note:
All of the nodes’ values will be unique.
p and q are different and both values will exist in the binary tree.
AC代码1
最简单粗暴的解法:从下往上依次遍历树中每个节点,第一次出现子树(包括节点本身)既有p又有q的结点为LCA所求结点。
时间复杂度:O(n * n)
class Solution {
private TreeNode resNode = null;
public TreeNode findCommonNode(TreeNode root, TreeNode p, TreeNode q){
if(root == null) return null;
if(p == null && q == null)
return null;
if(root.left != null) findCommonNode(root.left, p, q);
if(root.right != null) findCommonNode(root.right, p, q);
if(hasNode(root, p) && hasNode(root, q))
if(resNode == null) resNode = root;
return resNode;
}
private boolean hasNode(TreeNode root, TreeNode node){
if(root == null) return false;
if(root == node) return true;;
if(root.left == node || root.right == node) return true;
return hasNode(root.left, node) || hasNode(root.right, node);
}
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
return findCommonNode(root, p, q);
}
}
AC代码2
leetcode大神的解法,以递归的方式解决LCA问题。
时间复杂度:O(n);
空间复杂度:O(n)。主要是递归栈的深度。
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if(root == null || root == p || root == q) {
return root;
}
TreeNode left = lowestCommonAncestor(root.left, p , q);
TreeNode right = lowestCommonAncestor(root.right, p , q);
return left == null ? right: right == null ? left: root;
}
}
AC代码3
建立两队列,一个队列保存从根节点到p的路径,另一个队列保存从根节点到q的路径。两个路径中第一个不相同的节点的前一个节点即为LCA所求。
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if(root == null || root == p || root == q) {
return root;
}
LinkedList<TreeNode> q1 = new LinkedList<>();
LinkedList<TreeNode> q2 = new LinkedList<>();
getTrace(root, q1, p);
getTrace(root, q2, q);
return LCA(q1, q2);
}
private boolean getTrace(TreeNode root, LinkedList<TreeNode> queue, TreeNode node){
if(root == null) return false;
if(root == node){
queue.offer(root);
return true;
}
queue.offer(root);
if(getTrace(root.left, queue, node) || getTrace(root.right, queue, node)) return true;
queue.pollLast();
return false;
}
private TreeNode LCA(Queue<TreeNode> q1, Queue<TreeNode> q2){
TreeNode node = null;
while(true){
TreeNode n1 = q1.poll();
TreeNode n2 = q2.poll();
if(n1 == n2) node = n1;
else break;
}
return node;
}
}
AC代码4
离线Tarjan算法