-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathCommonAncester.cs
38 lines (37 loc) · 1.16 KB
/
CommonAncester.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Tree.TreeLib
{
public class CommonAncester
{
public TreeNode LowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q)
{
if (root == null)
return null;
if (p == root || q == root) //等于root,则最小祖先一定为root
return root;
bool pOnLeft = isOn(root.left, p);
bool qOnLeft = isOn(root.left, q);
if (pOnLeft != qOnLeft)
return root;
if (pOnLeft == true && qOnLeft == true)
return LowestCommonAncestor(root.left, p, q);
return LowestCommonAncestor(root.right, p, q);
}
//位于node上吗
private bool isOn(TreeNode node, TreeNode goal)
{
if (goal == null)
return true;
if (node == null)
return false;
if (node == goal)
return true;
if (isOn(node.left, goal) == true || isOn(node.right, goal) == true)
return true;
return false;
}
}
}