mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-02 02:02:57 +08:00
refactor: add/refactor method in include, simplified print code (#471)
This commit is contained in:
@@ -70,7 +70,7 @@ public class backtrack_find_constrained_paths
|
||||
[Test]
|
||||
public void Test()
|
||||
{
|
||||
TreeNode root = TreeNode.ArrToTree(new int?[] { 1, 7, 3, 4, 5, 6, 7 });
|
||||
TreeNode root = TreeNode.ListToTree(new List<int?> { 1, 7, 3, 4, 5, 6, 7 });
|
||||
Console.WriteLine("\n初始化二叉树");
|
||||
PrintUtil.PrintTree(root);
|
||||
|
||||
@@ -82,12 +82,7 @@ public class backtrack_find_constrained_paths
|
||||
Console.WriteLine("\n输出所有根节点到节点 7 的路径,要求路径中不包含值为 3 的节点");
|
||||
foreach (List<TreeNode> path in res)
|
||||
{
|
||||
List<int> vals = new List<int>();
|
||||
foreach (TreeNode node in path)
|
||||
{
|
||||
vals.Add(node.val);
|
||||
}
|
||||
Console.WriteLine(string.Join(" ", vals));
|
||||
PrintUtil.PrintList(path.Select(p => p.val).ToList());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -38,7 +38,7 @@ public class preorder_find_constrained_paths
|
||||
[Test]
|
||||
public void Test()
|
||||
{
|
||||
TreeNode root = TreeNode.ArrToTree(new int?[] { 1, 7, 3, 4, 5, 6, 7 });
|
||||
TreeNode root = TreeNode.ListToTree(new List<int?> { 1, 7, 3, 4, 5, 6, 7 });
|
||||
Console.WriteLine("\n初始化二叉树");
|
||||
PrintUtil.PrintTree(root);
|
||||
|
||||
@@ -50,12 +50,7 @@ public class preorder_find_constrained_paths
|
||||
Console.WriteLine("\n输出所有根节点到节点 7 的路径,且路径中不包含值为 3 的节点");
|
||||
foreach (List<TreeNode> path in res)
|
||||
{
|
||||
List<int> vals = new List<int>();
|
||||
foreach (TreeNode node in path)
|
||||
{
|
||||
vals.Add(node.val);
|
||||
}
|
||||
Console.WriteLine(string.Join(" ", vals));
|
||||
PrintUtil.PrintList(path.Select(p => p.val).ToList());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,7 @@
|
||||
|
||||
using hello_algo.include;
|
||||
using NUnit.Framework;
|
||||
using System.IO;
|
||||
|
||||
namespace hello_algo.chapter_backtracking;
|
||||
|
||||
@@ -32,7 +33,7 @@ public class preorder_find_nodes
|
||||
[Test]
|
||||
public void Test()
|
||||
{
|
||||
TreeNode root = TreeNode.ArrToTree(new int?[] { 1, 7, 3, 4, 5, 6, 7 });
|
||||
TreeNode root = TreeNode.ListToTree(new List<int?> { 1, 7, 3, 4, 5, 6, 7 });
|
||||
Console.WriteLine("\n初始化二叉树");
|
||||
PrintUtil.PrintTree(root);
|
||||
|
||||
@@ -41,11 +42,6 @@ public class preorder_find_nodes
|
||||
preOrder(root);
|
||||
|
||||
Console.WriteLine("\n输出所有值为 7 的节点");
|
||||
List<int> vals = new List<int>();
|
||||
foreach (TreeNode node in res)
|
||||
{
|
||||
vals.Add(node.val);
|
||||
}
|
||||
Console.WriteLine(string.Join(" ", vals));
|
||||
PrintUtil.PrintList(res.Select(p => p.val).ToList());
|
||||
}
|
||||
}
|
||||
@@ -37,7 +37,7 @@ public class preorder_find_paths
|
||||
[Test]
|
||||
public void Test()
|
||||
{
|
||||
TreeNode root = TreeNode.ArrToTree(new int?[] { 1, 7, 3, 4, 5, 6, 7 });
|
||||
TreeNode root = TreeNode.ListToTree(new List<int?> { 1, 7, 3, 4, 5, 6, 7 });
|
||||
Console.WriteLine("\n初始化二叉树");
|
||||
PrintUtil.PrintTree(root);
|
||||
|
||||
@@ -49,12 +49,7 @@ public class preorder_find_paths
|
||||
Console.WriteLine("\n输出所有根节点到节点 7 的路径");
|
||||
foreach (List<TreeNode> path in res)
|
||||
{
|
||||
List<int> vals = new List<int>();
|
||||
foreach (TreeNode node in path)
|
||||
{
|
||||
vals.Add(node.val);
|
||||
}
|
||||
Console.WriteLine(string.Join(" ", vals));
|
||||
PrintUtil.PrintList(path.Select(p => p.val).ToList());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user