Day10
This commit is contained in:
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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user