public IList <int> PreorderTraversal(TreeNode root) { SimpleStack <TreeNode> stack = new SimpleStack <TreeNode> (); IList <int> result = new List <int> (); if (root != null) { stack.Push(root); while (!stack.Empty()) { var node = stack.Pop(); result.Add(node.val); if (node.right != null) { stack.Push(node.right); } if (node.left != null) { stack.Push(node.left); } } } return(result); }
private void Push(TreeNode node, SimpleStack <TreeNode> stack) { while (node != null) { stack.Push(node); node = node.left; } }
private void PushToStack(TreeNode root, SimpleStack <TreeNode> stack) { while (root != null) { stack.Push(root); root = root.left; } }
public int MaxSub(int[] nums) { SimpleStack <int> stack = new SimpleStack <int> (); var lastMax = int.MinValue; for (int i = 0; i < nums.Length; i++) { if (nums [i] < 0) { if (lastMax <= nums [i]) { lastMax = nums [i]; } if (!stack.Empty()) { var last = stack.Pop(); lastMax = Math.Max(lastMax, last); last += nums [i]; if (last > 0) { stack.Push(last); } } } else if (nums [i] > 0) { if (stack.Empty()) { stack.Push(nums [i]); } else { var last = stack.Pop(); last += nums [i]; stack.Push(last); } } else { lastMax = Math.Max(nums [i], lastMax); } } if (!stack.Empty()) { var f = stack.Pop(); lastMax = Math.Max(lastMax, f); } return(lastMax); }
public void DFS(int start, List <int>[] adj, SimpleStack <int> stack, bool[] visited) { visited [start] = true; if (adj[start] != null) { foreach (var r in adj[start]) { if (!visited [r]) { DFS(r, adj, stack, visited); } } } stack.Push(start); }
public void NextPermutation(int[] nums) { if (nums != null && nums.Length > 1) { var stack = new SimpleStack <Tuple <int, int> >(); stack.Push(new Tuple <int, int>(nums[nums.Length - 1], nums.Length - 1)); int start = 0; for (int i = nums.Length - 2; i >= 0; i--) { Tuple <int, int> replace = null; while (!stack.Empty() && stack.Top().Item1 > nums[i]) { replace = stack.Pop(); } if (replace != null) { Swap(nums, i, replace.Item2); start = i + 1; break; } else { if (nums[i] > stack.Top().Item1) { stack.Push(new Tuple <int, int>(nums[i], i)); } } } for (int i = 0; i <= (nums.Length - 1 - start) / 2; i++) { Swap(nums, start + i, nums.Length - 1 - i); } } }
public int[] FindOrder(int numCourses, int[,] prerequisites) { List <int>[] adj = new List <int> [numCourses]; for (int r = 0; r < prerequisites.GetLength(0); r++) { if (adj [prerequisites [r, 1]] == null) { adj [prerequisites [r, 1]] = new List <int> (); } adj [prerequisites [r, 1]].Add(prerequisites [r, 0]); } var stack = new SimpleStack <int> (); bool[] visited = new bool[numCourses]; int i = 0; for (; i < numCourses; i++) { if (!visited [i]) { DFS(i, adj, stack, visited); } } var result = new int[numCourses]; i = 0; while (!stack.Empty()) { result [i++] = stack.Pop(); } return(result); }
public IList <int> InorderTraversal(TreeNode root) { var stack = new SimpleStack <TreeNode> (); var result = new List <int> (); if (root != null) { Push(root, stack); while (!stack.Empty()) { var n = stack.Pop(); result.Add(n.val); if (n.right != null) { Push(n.right, stack); } } } return(result); }
public IList <IList <int> > ZigzagLevelOrder(TreeNode root) { var lrstack = new SimpleStack <TreeNode> (); var rlstack = new SimpleStack <TreeNode> (); List <IList <int> > result = new List <IList <int> > (); if (root == null) { return(result); } else { lrstack.Push(root); result.Add(new List <int> () { root.val }); var cur = lrstack; while (!cur.Empty()) { var r = new List <int> (); while (!cur.Empty()) { var n = cur.Pop(); if (cur == rlstack) { if (n.left != null) { r.Add(n.left.val); lrstack.Push(n.left); } if (n.right != null) { r.Add(n.right.val); lrstack.Push(n.right); } } else { if (n.right != null) { r.Add(n.right.val); rlstack.Push(n.right); } if (n.left != null) { r.Add(n.left.val); rlstack.Push(n.left); } } } if (r.Count > 0) { result.Add(r); } if (cur == rlstack) { cur = lrstack; } else { cur = rlstack; } } } return(result); }