Пример #1
0
            public IList <IList <int> > LevelOrder(ChildrenNode root)
            {
                IList <IList <int> > result = new List <IList <int> >();

                if (root == null)
                {
                    return(result);
                }

                Queue <ChildrenNode> queue = new Queue <ChildrenNode>();

                queue.Enqueue(root);

                while (queue.Count != 0)
                {
                    int         level   = queue.Count;
                    IList <int> subList = new List <int>();
                    for (int i = 0; i < level; i++)
                    {
                        ChildrenNode node = queue.Dequeue();
                        subList.Add(node.val);

                        foreach (ChildrenNode child in node.children)
                        {
                            queue.Enqueue(child);
                        }
                    }
                    result.Add(subList);
                }
                return(result);
            }
Пример #2
0
            public IList <int> Preorder(ChildrenNode root)
            {
                IList <int> preorder = new List <int>();

                Traverse(root, preorder);
                return(preorder);
            }
Пример #3
0
            private void Helper(ChildrenNode root, int level,
                                List <IList <int> > result)
            {
                if (root == null)
                {
                    return;
                }

                if (result.Count - 1 < level)
                {
                    result.Add(new List <int>());
                    result[level].Add(root.val);
                }
                else
                {
                    result[level].Add(root.val);
                }

                if (root.children != null)
                {
                    foreach (var node in root.children)
                    {
                        Helper(node, level + 1, result);
                    }
                }
            }
Пример #4
0
            public IList <IList <int> > LevelOrder(ChildrenNode root)
            {
                List <IList <int> > result = new List <IList <int> >();

                Helper(root, 0, result);
                return(result);
            }
Пример #5
0
            public IList <int> Postorder(ChildrenNode root)
            {
                IList <int> postorder = new List <int>();

                DetermineOrder(root, postorder);
                return(postorder);
            }
Пример #6
0
            public int MaxDepth(ChildrenNode root)
            {
                Queue <ChildrenNode> q1 = new Queue <ChildrenNode>();
                Queue <ChildrenNode> q2 = new Queue <ChildrenNode>();

                int depth = 0;

                if (root == null)
                {
                    return(depth);
                }

                q1.Enqueue(root);
                while (q1.Count > 0)
                {
                    while (q1.Count > 0)
                    {
                        ChildrenNode curr = q1.Dequeue();
                        if (curr.children != null)
                        {
                            foreach (ChildrenNode child in curr.children)
                            {
                                q2.Enqueue(child);
                            }
                        }
                    }
                    depth++;
                    q1 = q2;
                    q2 = new Queue <ChildrenNode>();
                }

                return(depth);
            }
Пример #7
0
            private void Traverse(ChildrenNode node, IList <int> preorder)
            {
                if (node == null)
                {
                    return;
                }

                preorder.Add(node.val);

                foreach (ChildrenNode currNode in node.children)
                {
                    Traverse(currNode, preorder);
                }
            }
Пример #8
0
            private void DetermineOrder(ChildrenNode root, IList <int> postorder)
            {
                if (root == null)
                {
                    return;
                }

                if (root.children != null && root.children.Count > 0)
                {
                    for (int i = 0; i < root.children.Count; i++)
                    {
                        DetermineOrder(root.children[i], postorder);
                    }
                }

                postorder.Add(root.val);
            }