예제 #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);
            }