-
Notifications
You must be signed in to change notification settings - Fork 56
/
69.MiniDistanceTwoTreeNodes.cs
124 lines (108 loc) · 2.95 KB
/
69.MiniDistanceTwoTreeNodes.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
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
//Find minimum distance between any 2 given nodes in Binary Tree
//First find the Lowest Common Ancestor of the 2 given nodes
//Then find the minimum distance between 2 nodes.
//The minimum path from one node to another must pass their lowest
//common ancestor
using System;
using System.Collections.Generic;
using System.Collections;
namespace MinimumDistanceTreeNodes
{
public class TreeNode
{
public int Value{ get; private set;}
public TreeNode Left { get; set;}
public TreeNode Right { get; set;}
public TreeNode(int value, TreeNode left, TreeNode right)
{
this.Value = value;
this.Left = left;
this.Right = right;
}
public TreeNode (int value)
{
this.Value = value;
this.Left = null;
this.Right = null;
}
}
public class Finder
{
public static TreeNode FindLCA(TreeNode root, TreeNode p, TreeNode q)
{
if(root == null || p == null || q == null)
return null;
if (root == p || root == q)
return root;
else {
TreeNode l = FindLCA (root.Left, p, q); //try to get LCA of p,q in left subtree
TreeNode r = FindLCA (root.Right, p, q); //try to get LCA of p,q in right subtree
if (l != null && r != null) //p,q are at different sides of root
return root;
if (l == null) //p,q are both at the right side of root
return r;
else
return l; //p,q are both at the left side of root
}
}
public static int FindMinDis (TreeNode root, TreeNode p, TreeNode q)
{
TreeNode troot = FindLCA (root, p, q);
int countp = FindLevelNum (troot, p);
int countq = FindLevelNum (troot, q);
return countp + countq;
}
public static void Swap<T> (ref T lhs, ref T rhs)
{
T temp;
temp = lhs;
lhs = rhs;
rhs = temp;
}
public static int FindLevelNum(TreeNode root, TreeNode p) //use two queues
{
Queue<TreeNode> currentLevel = new Queue<TreeNode> ();
Queue<TreeNode> nextLevel = new Queue<TreeNode> ();
int count = 0;
currentLevel.Enqueue (root);
while (currentLevel.Count != 0) {
TreeNode currentNode = currentLevel.Peek ();
currentLevel.Dequeue ();
if (currentNode != null) {
if (currentNode == p) {
return count;
}
nextLevel.Enqueue(currentNode.Left);
nextLevel.Enqueue(currentNode.Right);
}
if (currentLevel.Count == 0) {
count++;
Swap (ref currentLevel, ref nextLevel);
}
}
return 0;
}
}
class MainClass
{
public static void Main (string[] args)
{
TreeNode Node1 = new TreeNode (3);
TreeNode Node2 = new TreeNode (9);
TreeNode Node3 = new TreeNode (20);
TreeNode Node4 = new TreeNode (15);
TreeNode Node5 = new TreeNode (17);
TreeNode Node6 = new TreeNode (11);
TreeNode Node7 = new TreeNode (13);
TreeNode Node8 = new TreeNode (71);
Node1.Left = Node2;
Node1.Right = Node3;
Node3.Left = Node4;
Node3.Right = Node5;
Node4.Left = Node6;
Node4.Right = Node7;
Node5.Left = Node8;
Console.WriteLine (Finder.FindMinDis (Node1, Node5, Node6));
}
}
}