Day10
This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<RootNamespace>_145._Binary_Tree_Postorder_Traversal</RootNamespace>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
9
145. Binary Tree Postorder Traversal/Program.cs
Normal file
9
145. Binary Tree Postorder Traversal/Program.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
namespace _145._Binary_Tree_Postorder_Traversal;
|
||||
|
||||
internal class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
Console.WriteLine("Hello, World!");
|
||||
}
|
||||
}
|
||||
24
145. Binary Tree Postorder Traversal/Solution.cs
Normal file
24
145. Binary Tree Postorder Traversal/Solution.cs
Normal file
@@ -0,0 +1,24 @@
|
||||
namespace _145._Binary_Tree_Postorder_Traversal;
|
||||
|
||||
public class Solution
|
||||
{
|
||||
public IList<int> PostorderTraversal(TreeNode root)
|
||||
{
|
||||
var result = new List<int>();
|
||||
if (root == null) return result;
|
||||
var stack = new Stack<TreeNode>();
|
||||
stack.Push(root);
|
||||
while (stack.Any())
|
||||
{
|
||||
var cur = stack.Pop();
|
||||
result.Add(cur.val);
|
||||
if (cur.left != null)
|
||||
stack.Push(cur.left);
|
||||
if (cur.right != null)
|
||||
stack.Push(cur.right);
|
||||
}
|
||||
result.Reverse();
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
16
145. Binary Tree Postorder Traversal/TreeNode.cs
Normal file
16
145. Binary Tree Postorder Traversal/TreeNode.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
namespace _145._Binary_Tree_Postorder_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