private int GetSum(NestedInteger nestedIntegerInput, int depth) { int result = 0; LinkedList <(NestedInteger, int)> queue = new LinkedList <(NestedInteger, int)>(); queue.AddLast((nestedIntegerInput, depth)); while (queue.Count != 0) { (NestedInteger ni, int cDepth) = queue.First.Value; queue.RemoveFirst(); if (ni.IsInteger()) { result += ni.GetInteger() * cDepth; } else { foreach (var item in ni.GetList()) { queue.AddLast((item, cDepth - 1)); } } } return(result); }
private int GetDepth(NestedInteger nesteInteger) { int maxDepth = 1; LinkedList <(NestedInteger, int)> queue = new LinkedList <(NestedInteger, int)>(); queue.AddLast((nesteInteger, maxDepth)); while (queue.Count != 0) { (NestedInteger ni, int depth) = queue.First.Value; queue.RemoveFirst(); if (ni.IsInteger()) { maxDepth = Math.Max(maxDepth, depth); } else { foreach (var item in ni.GetList()) { queue.AddLast((item, depth + 1)); } } } return(maxDepth); }
// Set this NestedInteger to hold a nested list and adds a nested integer to it. public void Add(NestedInteger ni) { if (IsInteger() || values == null) { value = null; values = new List <NestedInteger>(); } values.Add(ni); }