Skip to content

Commit 950f32d

Browse files
author
atwood
committed
review
1 parent dd892ef commit 950f32d

File tree

3 files changed

+104
-39
lines changed

3 files changed

+104
-39
lines changed

INo64_Math.cs

+5
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ namespace LeetCode_I64
2020
// }
2121
//}
2222

23+
/// <summary>
24+
/// REVIEW
25+
/// 2020.08.03: 要求的难点就是不能使用if等关键字,但是其实也是用了,只不过是用了另外的方法躲开了检测而已。
26+
/// </summary>
27+
2328
/// <summary>
2429
/// Unsolved Question: 为什么使用了类中全局的变量,避免了递归每次申请变量,反而内存消耗变大了。这个递归应该会申请很多个bool才对啊
2530
/// </summary>

No114_Tree.cs

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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+
}

Program.cs

+3-39
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ static void Main(string[] args)
2121
//string input2 = Console.ReadLine();
2222
//int[] intArr = input.Split(',').Select(s => int.Parse(s)).ToArray();
2323
//int input2 = int.Parse(Console.ReadLine());
24-
var builder = new DataStructureBuilder();
25-
int?[] data = new int?[] { 1, 2, 5, 3, 4, null, 6 };
26-
var tree = builder.BuildTree(data);
24+
//var builder = new DataStructureBuilder();
25+
//int?[] data = new int?[] { 1, 2, 5, 3, 4, null, 6 };
26+
//var tree = builder.BuildTree(data);
2727
//var listNode = builder.BuildListNode(new int[] { 1, 4, 5 });
2828
//int[][] arr = new int[3][] { new int[] { 1, 3, 1 }, new int[] { 1, 5, 1 }, new int[] { 4, 2, 1 } };
2929
//int[] nums1 = new int[] { 2, 1, -2, 3 };
@@ -32,45 +32,9 @@ static void Main(string[] args)
3232
//string input2 = "*a*b";
3333
//var res = solution.IsMatch(input, input2);
3434
//ConsoleX.WriteLine(res);
35-
solution.Flatten(tree);
3635
}
3736
}
3837

39-
public class Solution
40-
{
41-
/// <summary>
42-
/// 运用了迭代中序遍历的代码,先把节点都入队,然后串联成目标树就可以了
43-
/// 时间复杂度:O(n)
44-
/// 空间复杂度:O(n)
45-
/// </summary>
46-
/// <param name="root"></param>
47-
public void Flatten(TreeNode root)
48-
{
49-
Queue<TreeNode> queue = new Queue<TreeNode>();
50-
Stack<TreeNode> stack = new Stack<TreeNode>();
51-
TreeNode curr = root;
52-
while (curr != null || stack.Count > 0)
53-
{
54-
while (curr != null)
55-
{
56-
queue.Enqueue(curr);
57-
stack.Push(curr);
58-
curr = curr.left;
59-
}
60-
curr = stack.Pop();
61-
curr = curr.right;
62-
}
63-
if (queue.Count != 0)
64-
root = queue.Dequeue();
65-
while (queue.Count != 0)
66-
{
67-
//这里有一点要注意的,一定要用root.right去串联,如果用root去串联的话,其实只是变量指向的空间而已,并没有串起来
68-
root.right = queue.Dequeue();
69-
root.left = null;
70-
root = root.right;
71-
}
72-
}
73-
}
7438
//public class Solution
7539
//{
7640
// public bool IsMatch(string s, string p)

0 commit comments

Comments
 (0)