Esempio n. 1
0
        public IList <int> NaryPreorderIter(NaryNode root)
        {
            var results = new List <int>();

            if (root == null)
            {
                return(results);
            }
            Stack <NaryNode> stack = new Stack <NaryNode>();

            stack.Push(root);
            while (stack.Count > 0)
            {
                var node = stack.Pop();
                results.Add(node.val);
                if (node.children == null)
                {
                    continue;
                }
                for (int i = node.children.Count - 1; i >= 0; i--)
                {
                    stack.Push(node.children[i]);
                }
            }
            return(results);
        }
Esempio n. 2
0
        //https://leetcode.com/problems/maximum-depth-of-n-ary-tree/
        public int MaxDepthNary(NaryNode root)
        {
            if (root == null)
            {
                return(0);
            }
            int level          = 0;
            Queue <NaryNode> q = new Queue <NaryNode>();

            q.Enqueue(root);
            while (q.Count > 0)
            {
                var count = q.Count;
                level += 1;
                for (int i = 0; i < count; i++)
                {
                    var node = q.Dequeue();
                    for (int j = 0; j < node.children.Count; j++)
                    {
                        q.Enqueue(node.children[j]);
                    }
                }
            }
            return(level);
        }
Esempio n. 3
0
        //https://leetcode.com/problems/n-ary-tree-postorder-traversal/
        public IList <int> NaryPostorder(NaryNode root)
        {
            if (root == null)
            {
                throw new ArgumentNullException(nameof(root));
            }

            IList <int>      results    = new List <int>();
            Stack <NaryNode> leftStack  = new Stack <NaryNode>();
            Stack <NaryNode> rightStack = new Stack <NaryNode>();

            leftStack.Push(root);
            while (leftStack.Count > 0)
            {
                var current = leftStack.Pop();
                for (int i = 0; i < current.children.Count; i++)
                {
                    leftStack.Push(current.children[i]);
                }

                rightStack.Push(current);
            }
            while (rightStack.Count > 0)
            {
                results.Add(rightStack.Pop().val);
            }

            return(results);
        }
Esempio n. 4
0
        public IList <int> NaryPreorder(NaryNode root)
        {
            var results = new List <int>();

            NaryPreorderWorker(root, results);
            return(results);
        }
Esempio n. 5
0
        public IList <int> NaryPostorderIter(NaryNode root)
        {
            var results = new List <int>();

            if (root == null)
            {
                return(results);
            }
            Stack <NaryNode> stackLeft  = new Stack <NaryNode>();
            Stack <int>      stackRight = new Stack <int>();

            stackLeft.Push(root);
            while (stackLeft.Count > 0)
            {
                var current = stackLeft.Pop();
                foreach (var item in current.children)
                {
                    stackLeft.Push(item);
                }
                stackRight.Push(current.val);
            }
            while (stackRight.Count > 0)
            {
                results.Add(stackRight.Pop());
            }
            return(results);
        }
Esempio n. 6
0
        public IList <int> NaryPreorder(NaryNode root)
        {
            IList <int> results = new List <int>();

            if (root != null)
            {
                NaryPreorderWorker(root, results);
            }
            return(results);
        }
Esempio n. 7
0
 void MaxDepthNaryWorker(NaryNode root)
 {
     if (root == null)
     {
         return;
     }
     MaxDepthNaryLevel += 1;
     for (int i = 0; i < root.children.Count; i++)
     {
         MaxDepthNary(root.children[i]);
     }
 }
Esempio n. 8
0
 void NaryPreorderWorker(NaryNode node, IList <int> results)
 {
     if (node == null)
     {
         return;
     }
     results.Add(node.val);
     for (int i = 0; i < node.children.Count; i++)
     {
         NaryPreorderWorker(node.children[i], results);
     }
     return;
 }
Esempio n. 9
0
 void NaryPreorderWorker(NaryNode node, IList <int> results)
 {
     if (node == null)
     {
         return;
     }
     results.Add(node.val);
     foreach (var item in node.children)
     {
         NaryPreorderWorker(item, results);
     }
     return;
 }
Esempio n. 10
0
        public int MaxDepthNary2(NaryNode root)
        {
            if (root == null)
            {
                return(0);
            }
            int max = 0;

            for (int i = 0; i < root.children.Count; i++)
            {
                max = Math.Max(max, MaxDepthNary2(root.children[i]));
            }
            return(max + 1);
        }
Esempio n. 11
0
        //https://leetcode.com/problems/n-ary-tree-preorder-traversal/
        public IList <int> NaryPreorderIter(NaryNode root)
        {
            IList <int> results = new List <int>();

            if (root == null)
            {
                return(results);
            }
            Stack <NaryNode> stack = new Stack <NaryNode>();

            stack.Push(root);
            while (stack.Count > 0)
            {
                var current = stack.Pop();
                results.Add(current.val);
                for (int i = current.children.Count - 1; i >= 0; i--)
                {
                    stack.Push(current.children[i]);
                }
            }
            return(results);
        }
Esempio n. 12
0
        public int MaxDepthNaryIter(NaryNode root)
        {
            var level = 0;

            if (root != null)
            {
                Queue <NaryNode> q = new Queue <NaryNode>();
                q.Enqueue(root);
                while (q.Count > 0)
                {
                    level += 1;
                    var count = q.Count;
                    for (int i = 0; i < count; i++)
                    {
                        var node = q.Dequeue();
                        foreach (NaryNode item in node.children)
                        {
                            q.Enqueue(item);
                        }
                    }
                }
            }
            return(level);
        }
Esempio n. 13
0
 public int MaxDepthNary(NaryNode root)
 {
     MaxDepthNaryWorker(root);
     return(MaxDepthNaryLevel);
 }