本文整理汇总了C#中TreeNode.Where方法的典型用法代码示例。如果您正苦于以下问题:C# TreeNode.Where方法的具体用法?C# TreeNode.Where怎么用?C# TreeNode.Where使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类TreeNode
的用法示例。
在下文中一共展示了TreeNode.Where方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
internal static void Main()
{
Console.WriteLine("N = ");
var n = int.Parse(Console.ReadLine());
var nodes = new TreeNode<int>[n];
for (int i = 0; i < n; i++)
{
nodes[i] = new TreeNode<int>(i);
}
for (int i = 0; i < n - 1; i++)
{
Console.WriteLine("Enter parent and child node [separated by spaces] ");
var input = Console.ReadLine().Split(' ');
var parentValue = int.Parse(input[0]);
var childValue = int.Parse(input[1]);
nodes[parentValue].AddChild(nodes[childValue]);
}
Console.WriteLine("Root node: ");
var rootNode = nodes.FirstOrDefault(x => x.HasParent == false);
if (rootNode != null)
{
Console.WriteLine(rootNode.Value);
}
Console.WriteLine("Leaf nodes: ");
var leafNodes = nodes.Where(x => x.Children.Count == 0).ToList();
leafNodes.ForEach(x => Console.WriteLine(x.Value));
Console.WriteLine("Middle nodes: ");
var middleNodes = nodes.Where(x => x.Children.Count != 0 && x.HasParent).ToList();
middleNodes.ForEach(x => Console.WriteLine(x.Value));
Console.WriteLine("Max depth: " + TreeDepthCalculate(rootNode));
}
示例2: Main
static void Main()
{
/* Paste sample input
Example input:
7
2 4
3 2
5 0
3 5
5 6
5 1
*/
int numberOfNodes = int.Parse(Console.ReadLine());
TreeNode<int>[] values = new TreeNode<int>[numberOfNodes];
for (int i = 0; i < numberOfNodes; i++)
{
values[i] = new TreeNode<int>(i);
}
for (int i = 0; i < numberOfNodes - 1; i++)
{
string edge = Console.ReadLine();
string[] parentChildPair = edge.Split(' ');
int parent = int.Parse(parentChildPair[0]);
int child = int.Parse(parentChildPair[1]);
values[parent].AddChild(values[child]);
}
// Problem 1a:
var root = values.Where(x => !x.HasParent).FirstOrDefault();
Console.WriteLine("Root: {0}", root.Value);
// Problem 1b:
var leaves = values.Where(x => x.ChildrenCount == 0).Select(x => x.Value);
Console.WriteLine("Leafe nodes: {0}", string.Join(", ", leaves));
// Problem 1c:
var middleNodes = values.Where(x => x.HasParent && x.ChildrenCount > 0).Select(x => x.Value);
Console.WriteLine("Middle nodes: {0}", string.Join(", ", middleNodes));
// Problem 1d:
Console.WriteLine("Longest path: {0}", FindLongestPath(root));
// Problem 1e:
int sum = 9;
var allPathsWithSumS = GetAllPathsWithGivenSum(sum, values, root);
Console.WriteLine();
foreach (var path in allPathsWithSumS)
{
Console.WriteLine("Path in tree with sum {0} is: {1}", sum, path);
}
var subtreeSum = 6;
var allSubTreesWithSumS = GetAllSubtreeWithGivenSum(subtreeSum, root);
Console.WriteLine();
foreach (var path in allSubTreesWithSumS)
{
Console.WriteLine("Subtree with sum {0} is: {1}", subtreeSum, path);
}
}