本文整理汇总了C#中TreeNode.GetChild方法的典型用法代码示例。如果您正苦于以下问题:C# TreeNode.GetChild方法的具体用法?C# TreeNode.GetChild怎么用?C# TreeNode.GetChild使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类TreeNode
的用法示例。
在下文中一共展示了TreeNode.GetChild方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: FindAllPathsWithGivenSum
private static List<List<string>> FindAllPathsWithGivenSum(TreeNode<int> tree, int targetSum)
{
var pathCollection = new List<List<string>>();
var currentSum = tree.Value;
for (int i = 0; i < tree.ChildrenCount; i++)
{
var currentPath = new List<string>();
currentPath.Add(tree.Value.ToString());
currentPath.Add(tree.GetChild(i).Value.ToString());
currentSum += tree.GetChild(i).Value;
if (currentSum == targetSum)
{
pathCollection.Add(currentPath);
}
else if (currentSum > targetSum)
{
var firstElement = currentPath.First();
currentSum -= int.Parse(firstElement);
currentPath.Remove(firstElement);
}
FindTreesWithGivenSum(tree.GetChild(i), targetSum);
}
return pathCollection;
}
示例2: FindTreesWithGivenSum
private static List<TreeNode<int>> FindTreesWithGivenSum(TreeNode<int> tree, int targetSum)
{
var treeCollection = new List<TreeNode<int>>();
var resultSum = 0;
for (int i = 0; i < tree.ChildrenCount; i++)
{
resultSum += tree.GetChild(i).Value;
}
if (resultSum == targetSum)
{
treeCollection.Add(tree);
}
for (int i = 0; i < tree.ChildrenCount; i++)
{
treeCollection.AddRange(FindTreesWithGivenSum(tree.GetChild(i), targetSum));
}
return treeCollection;
}
示例3: PrintDFS
/// <summary>Traverses and prints tree in
/// Depth-First Search (DFS) manner</summary>
/// <param name="root">the root of the tree to be
/// traversed</param>
/// <param name="spaces">the spaces used for
/// representation of the parent-child relation</param>
private static void PrintDFS(TreeNode<int> root, string spaces)
{
if (Tree.Root == null)
{
return;
}
Console.WriteLine(spaces + root.Value);
TreeNode<int> child = null;
for (int i = 0; i < root.ChildrenCount; i++)
{
child = root.GetChild(i);
PrintDFS(child, spaces + " ");
}
}