

pre-order Recursive
bt.PreOrder(root);
public void PreOrder(Node root)
{
Console.WriteLine(root.Data);
if (root.Left != null) PreOrder(root.Left);
if (root.Right != null) PreOrder(root.Right);
}

pre-order Stack

bt.PreOrder(root);
public void PreOrder(Node root) // 이진 트리 Stack pre-order
{
Stack<Node> nodes = new Stack<Node>();
nodes.Push(root);
while(nodes.Count > 0)
{
Node temp = nodes.Pop();
Console.WriteLine(temp.Data);
if(temp.Right != null)
{
nodes.Push(temp.Right);
}
if (temp.Left != null)
{
nodes.Push(temp.Left);
}
}
}

'C# > 수업 내용' 카테고리의 다른 글
| [C#] 이진 트리 post-order(후위 순회) (0) | 2021.09.30 |
|---|---|
| [C#] 이진 트리 in-order(중위 순회) (0) | 2021.09.30 |
| [C#] 배열 Binary Tree (0) | 2021.09.29 |
| [C#] 이진 트리 Left Skewed Tree, Recursive Print (0) | 2021.09.27 |
| [C#] 이진 트리 level order traversal (0) | 2021.09.27 |