|
| 1 | +using LeetCode.ExtensionFunction; |
| 2 | +using System; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Text; |
| 5 | + |
| 6 | +namespace LeetCode_114 |
| 7 | +{ |
| 8 | + //static void Main(string[] args) |
| 9 | + //{ |
| 10 | + // var solution = new Solution(); |
| 11 | + // while (true) |
| 12 | + // { |
| 13 | + // //int input = int.Parse(Console.ReadLine()); |
| 14 | + // //int input2 = int.Parse(Console.ReadLine()); |
| 15 | + // //string input = Console.ReadLine(); |
| 16 | + // //string input2 = Console.ReadLine(); |
| 17 | + // //int[] intArr = input.Split(',').Select(s => int.Parse(s)).ToArray(); |
| 18 | + // //int input2 = int.Parse(Console.ReadLine()); |
| 19 | + // var builder = new DataStructureBuilder(); |
| 20 | + // int?[] data = new int?[] { 1, 2, 5, 3, 4, null, 6 }; |
| 21 | + // var tree = builder.BuildTree(data); |
| 22 | + // //var listNode = builder.BuildListNode(new int[] { 1, 4, 5 }); |
| 23 | + // //int[][] arr = new int[3][] { new int[] { 1, 3, 1 }, new int[] { 1, 5, 1 }, new int[] { 4, 2, 1 } }; |
| 24 | + // //int[] nums1 = new int[] { 2, 1, -2, 3 }; |
| 25 | + // //int[] nums2 = new int[] { 10, 15, 20 }; |
| 26 | + // //string input = "adceb"; |
| 27 | + // //string input2 = "*a*b"; |
| 28 | + // //var res = solution.IsMatch(input, input2); |
| 29 | + // //ConsoleX.WriteLine(res); |
| 30 | + // solution.Flatten(tree); |
| 31 | + // } |
| 32 | + //} |
| 33 | + |
| 34 | + public class Solution |
| 35 | + { |
| 36 | + /// <summary> |
| 37 | + /// 运用了迭代中序遍历的代码,先把节点都入队,然后串联成目标树就可以了 |
| 38 | + /// 时间复杂度:O(n) |
| 39 | + /// 空间复杂度:O(n) |
| 40 | + /// </summary> |
| 41 | + /// <param name="root"></param> |
| 42 | + public void Flatten(TreeNode root) |
| 43 | + { |
| 44 | + Queue<TreeNode> queue = new Queue<TreeNode>(); |
| 45 | + Stack<TreeNode> stack = new Stack<TreeNode>(); |
| 46 | + TreeNode curr = root; |
| 47 | + while (curr != null || stack.Count > 0) |
| 48 | + { |
| 49 | + while (curr != null) |
| 50 | + { |
| 51 | + queue.Enqueue(curr); |
| 52 | + stack.Push(curr); |
| 53 | + curr = curr.left; |
| 54 | + } |
| 55 | + curr = stack.Pop(); |
| 56 | + curr = curr.right; |
| 57 | + } |
| 58 | + if (queue.Count != 0) |
| 59 | + root = queue.Dequeue(); |
| 60 | + while (queue.Count != 0) |
| 61 | + { |
| 62 | + //这里有一点要注意的,一定要用root.right去串联,如果用root去串联的话,其实只是变量指向的空间而已,并没有串起来 |
| 63 | + root.right = queue.Dequeue(); |
| 64 | + root.left = null; |
| 65 | + root = root.right; |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + /// <summary> |
| 70 | + /// 类似莫里斯遍历的方法,通过直接改变树结构的方式完成 |
| 71 | + /// 时间复杂度:O(n) |
| 72 | + /// 空间复杂度:O(1) |
| 73 | + /// </summary> |
| 74 | + /// <param name="root"></param> |
| 75 | + //public void Flatten(TreeNode root) |
| 76 | + //{ |
| 77 | + // TreeNode curr = root; |
| 78 | + // while (curr != null) |
| 79 | + // { |
| 80 | + // if (curr.left != null) |
| 81 | + // { |
| 82 | + // TreeNode next = curr.left; |
| 83 | + // TreeNode predecessor = next; |
| 84 | + // while (predecessor.right != null) |
| 85 | + // { |
| 86 | + // predecessor = predecessor.right; |
| 87 | + // } |
| 88 | + // predecessor.right = curr.right; |
| 89 | + // curr.left = null; |
| 90 | + // curr.right = next; |
| 91 | + // } |
| 92 | + // curr = curr.right; |
| 93 | + // } |
| 94 | + //} |
| 95 | + } |
| 96 | +} |
0 commit comments