Day10
This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<RootNamespace>_144._Binary_Tree_Preorder_Traversal</RootNamespace>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
9
144. Binary Tree Preorder Traversal/Program.cs
Normal file
9
144. Binary Tree Preorder Traversal/Program.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
namespace _144._Binary_Tree_Preorder_Traversal;
|
||||
|
||||
internal class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
Console.WriteLine("Hello, World!");
|
||||
}
|
||||
}
|
||||
22
144. Binary Tree Preorder Traversal/Solution.cs
Normal file
22
144. Binary Tree Preorder Traversal/Solution.cs
Normal file
@@ -0,0 +1,22 @@
|
||||
namespace _144._Binary_Tree_Preorder_Traversal;
|
||||
|
||||
public class Solution
|
||||
{
|
||||
public IList<int> PreorderTraversal(TreeNode root)
|
||||
{
|
||||
List<int> res = new List<int>();
|
||||
Stack<TreeNode> stack = new Stack<TreeNode>();
|
||||
if (root != null)
|
||||
stack.Push(root);
|
||||
while (stack.Count > 0)
|
||||
{
|
||||
TreeNode cur = stack.Pop();
|
||||
if (cur.right != null)
|
||||
stack.Push(cur.right);
|
||||
if (cur.left != null)
|
||||
stack.Push(cur.left);
|
||||
res.Add(cur.val);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
||||
16
144. Binary Tree Preorder Traversal/TreeNode.cs
Normal file
16
144. Binary Tree Preorder Traversal/TreeNode.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
namespace _144._Binary_Tree_Preorder_Traversal;
|
||||
|
||||
|
||||
//Definition for a binary tree node.
|
||||
public class TreeNode
|
||||
{
|
||||
public int val;
|
||||
public TreeNode? left;
|
||||
public TreeNode? right;
|
||||
public TreeNode(int val = 0, TreeNode? left = null, TreeNode? right = null)
|
||||
{
|
||||
this.val = val;
|
||||
this.left = left;
|
||||
this.right = right;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user