public static IList <int> PreOrderNonRecursive(LeetCode589Node root) { Stack <LeetCode589Node> stack = new Stack <LeetCode589Node>(); IList <int> results = new List <int>(); if (root == null) { return(results); } stack.Push(root); while (stack.Count != 0) { var node = stack.Pop(); results.Add(node.val); var temp = (List <LeetCode589Node>)node.children; temp.Reverse(); foreach (var child in temp) { stack.Push(child); } } return(results); }
private static void PreOrder(LeetCode589Node root) { if (root == null) { return; } results.Add(root.val); foreach (var child in root.children) { PreOrder(child); } }
public static IList <int> PreOrderTraversal(LeetCode589Node root) { PreOrder(root); return(results); }