当前位置: 首页>>代码示例>>C#>>正文


C# TreeNode.GetChild方法代码示例

本文整理汇总了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;
        }
开发者ID:ViktorBarzin,项目名称:Telerik-Academy-2015-2016,代码行数:26,代码来源:Gardener.cs

示例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;
        }
开发者ID:ViktorBarzin,项目名称:Telerik-Academy-2015-2016,代码行数:21,代码来源:Gardener.cs

示例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 + "   ");
            }
        }
开发者ID:Huai-Ming,项目名称:Algorithms-Implementation,代码行数:22,代码来源:DFSHelper.cs


注:本文中的TreeNode.GetChild方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。