-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrintCommonPath.cs
More file actions
59 lines (50 loc) · 1.77 KB
/
Copy pathPrintCommonPath.cs
File metadata and controls
59 lines (50 loc) · 1.77 KB
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
using DataStructures.Libraries.Trees;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Algorithms.Problem.BinaryTree
{
[TestClass]
public class PrintCommonPath
{
public void PrintCommonNodes()
{
// Find LCA of two nodes.
// Get all the ancestors till the given node
}
public void PrintAllAncestors(BinaryTreeNode root, BinaryTreeNode node, List<double> tempValues, List<double> result)
{
if (root == null) return;
tempValues.Add(root.Value);
if (root.Value == node.Value)
{
// print the nodes
for(int i=0;i<tempValues.Count -1; i++)
{
result.Add(tempValues[i]);
System.Diagnostics.Debug.WriteLine(tempValues[i]);
}
result.Reverse();
}
this.PrintAllAncestors(root.Left, node, tempValues, result);
this.PrintAllAncestors(root.Right, node, tempValues, result);
}
[TestMethod]
public void TestPrintAllAncestors()
{
BinaryTreeNode root = new BinaryTreeNode(1);
root.Left = new BinaryTreeNode(2);
root.Right = new BinaryTreeNode(3);
root.Left.Left = new BinaryTreeNode(4);
root.Left.Right = new BinaryTreeNode(5);
root.Left.Left.Left = new BinaryTreeNode(7);
BinaryTreeNode node = root.Left.Left.Left;
var values = new List<double>();
List<double> result = new List<double>();
this.PrintAllAncestors(root, node, values, result);
}
}
}