-
Notifications
You must be signed in to change notification settings - Fork 6
/
LowestCommonAncestorOfBinaryTree.java
72 lines (70 loc) · 1.91 KB
/
LowestCommonAncestorOfBinaryTree.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package io.ziheng.codinginterviews;
/**
* 二叉树节点
*/
class TreeNode {
public int val;
public TreeNode left;
public TreeNode right;
public TreeNode(int val) {
this.val = val;
this.left = null;;
this.right = null;;
}
}
/**
* 剑指 Offer 面试题 68:二叉树两节点最小公共祖先
*
* 题目描述:
* 输入一棵二叉搜索树根节点与二叉树树中两个子节点,两节点的最小公共祖先。
*
* 知识点:["树"]
*/
public class LowestCommonAncestorOfBinaryTree {
/**
* 主函数 -> 测试用例
*
* @param args
* @return void
*/
public static void main(String[] args) {
LowestCommonAncestorOfBinaryTree obj = new LowestCommonAncestorOfBinaryTree();
TreeNode t1 = new TreeNode(1);
TreeNode t2 = new TreeNode(2);
TreeNode t3 = new TreeNode(3);
TreeNode t4 = new TreeNode(4);
TreeNode t5 = new TreeNode(5);
t3.left = t4;
t3.right = t5;
t1.left = t2;
t1.right = t3;
TreeNode root = t1;
System.out.println(
obj.lowestCommonAncestor(root, t4, t2).val
);
}
/**
* 剑指 Offer 面试题 68:二叉树两节点最小公共祖先
*
* 时间复杂度:O(n)
* 空间复杂度:O(n)
*
* @param rootNode
* @param pNode
* @param qNode
* @return TreeNode
*/
public TreeNode lowestCommonAncestor(
TreeNode rootNode, TreeNode pNode, TreeNode qNode) {
if (rootNode == null
|| rootNode == pNode
|| rootNode == qNode) {
return rootNode;
}
TreeNode leftNode = lowestCommonAncestor(rootNode.left, pNode, qNode);
TreeNode rightNode = lowestCommonAncestor(rootNode.right, pNode, qNode);
return leftNode == null ? rightNode
: rightNode == null ? leftNode : rootNode;
}
}
/* EOF */