本文整理汇总了C#中Tree.FindLongestPath方法的典型用法代码示例。如果您正苦于以下问题:C# Tree.FindLongestPath方法的具体用法?C# Tree.FindLongestPath怎么用?C# Tree.FindLongestPath使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Tree
的用法示例。
在下文中一共展示了Tree.FindLongestPath方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
static void Main()
{
Node<int> root = new Node<int>(5);
Tree<int> tree = new Tree<int>(root);
tree.Add(new Node<int>(7));
tree.Add(new Node<int>(1));
tree.Add(new Node<int>(2));
tree.Add(new Node<int>(9));
tree.Add(new Node<int>(4));
tree.Add(new Node<int>(8));
tree.Add(new Node<int>(0));
tree.Add(new Node<int>(11));
tree.Add(new Node<int>(-3));
tree.Add(new Node<int>(-6));
tree.Add(new Node<int>(-5));
tree.Add(new Node<int>(-8));
tree.Add(new Node<int>(5));
tree.Add(new Node<int>(12));
tree.Print();
tree.FindLongestPath();
Console.WriteLine("++++++++++++++++++++++++++++");
var allSubTreessWithSum = tree.FindSubtreesWithSum(-10);
foreach (var subTree in allSubTreessWithSum)
{
subTree.Print();
}
}
示例2: Main
public static void Main()
{
int N = int.Parse(Console.ReadLine());
TreeNode<int>[] nodes = SaveNodes(N);
GenNodesConnections(N, nodes);
Tree<int> tree = new Tree<int>(nodes);
// Task 1
TreeNode<int> root = tree.Root;
Console.WriteLine("Root : {0}", root.Value);
Console.WriteLine(new string('-', 40));
// Task 2
List<TreeNode<int>> leafs = tree.FindAllLeafs();
Print(leafs, "All leafs : ");
Console.WriteLine(new string('-', 40));
// Task 3
List<TreeNode<int>> middleNodes = tree.FindAllMiddleNodes();
Print(middleNodes, "All middle nodes : ");
Console.WriteLine(new string('-', 40));
// Task 4
int longestPath = tree.FindLongestPath(root);
Console.WriteLine("The longest path : {0}", longestPath);
Console.WriteLine(new string('-', 40));
int S = 10;
// Task 5
tree.PrintAllPathsWithSumS(S);
// Task 6
tree.PrintAllSubTreesWithSumS(S);
}
示例3: Main
public static void Main()
{
#if DEBUG
Console.SetIn(new StringReader(@"7
2 4
3 2
5 0
3 5
5 6
5 1"));
#endif
int inputSize = int.Parse(Console.ReadLine());
IList<TreeNode<int>> neighboursList = new TreeNode<int>[inputSize];
for (int i = 0; i < neighboursList.Count; i++)
{
neighboursList[i] = new TreeNode<int>(i);
}
for (int i = 0; i < inputSize - 1; i++)
{
string[] inputValue = Console.ReadLine().Split(' ');
//TreeNode<int> parent = new TreeNode<int>(int.Parse(inputValue[0]));
//TreeNode<int> child = new TreeNode<int>(int.Parse(inputValue[1]));
int parent = int.Parse(inputValue[0]);
int child = int.Parse(inputValue[1]);
//parent.AddChild(child);
neighboursList[parent].AddChild(neighboursList[child]);
}
TreeNode<int> root = new TreeNode<int>(0);
for (int i = 0; i < neighboursList.Count; i++)
{
if (neighboursList[i].HasParent == false)
{
//root.Data = neighboursList[i].Data;
root = neighboursList[i];
break;
}
}
Tree<int> testTree = new Tree<int>(root);
Console.WriteLine();
testTree.TraverseWithDFS();
//Find all leaves
List<TreeNode<int>> leaves = new List<TreeNode<int>>();
testTree.FindAllLeaves(leaves);
Console.Write("Leaves: ");
foreach (var leaf in leaves)
{
Console.Write(leaf.Data + " ");
}
Console.WriteLine();
//Find all middle nodes
List<TreeNode<int>> middleNodes = new List<TreeNode<int>>();
testTree.FindAllMiddleNodes(middleNodes);
Console.Write("Middle nodes: ");
foreach (var node in middleNodes)
{
Console.Write(node.Data + " ");
}
Console.WriteLine();
//Find longest path
int longestPath = testTree.FindLongestPath();
Console.WriteLine(longestPath);
//Find path with sum
int targetSum = 14;
List<List<TreeNode<int>>> paths = testTree.FindAllPathsWithSum(targetSum);
Console.WriteLine("Paths with sum of {0}:", targetSum);
foreach (var list in paths)
{
foreach (var item in list)
{
Console.Write(item.Data + " ");
}
Console.WriteLine();
}
}