Day10
This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<RootNamespace>_94._Binary_Tree_Inorder_Traversal</RootNamespace>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
9
94. Binary Tree Inorder Traversal/Program.cs
Normal file
9
94. Binary Tree Inorder Traversal/Program.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
namespace _94._Binary_Tree_Inorder_Traversal;
|
||||
|
||||
internal class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
Console.WriteLine("Hello, World!");
|
||||
}
|
||||
}
|
||||
23
94. Binary Tree Inorder Traversal/Solution.cs
Normal file
23
94. Binary Tree Inorder Traversal/Solution.cs
Normal file
@@ -0,0 +1,23 @@
|
||||
namespace _94._Binary_Tree_Inorder_Traversal;
|
||||
|
||||
public class Solution
|
||||
{
|
||||
public List<int> inorderTraversal(TreeNode root)
|
||||
{
|
||||
List<int> res = new List<int>();
|
||||
Stack<TreeNode> stack = new Stack<TreeNode>();
|
||||
TreeNode? curr = root;
|
||||
while (curr != null || stack.Count > 0)
|
||||
{
|
||||
while (curr != null)
|
||||
{
|
||||
stack.Push(curr);
|
||||
curr = curr.left;
|
||||
}
|
||||
curr = stack.Pop();
|
||||
res.Add(curr.val);
|
||||
curr = curr.right!;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
||||
15
94. Binary Tree Inorder Traversal/TreeNode.cs
Normal file
15
94. Binary Tree Inorder Traversal/TreeNode.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
namespace _94._Binary_Tree_Inorder_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