首页 > 代码库 > 层序遍历

层序遍历

private void LevelOrder(TreeNode node)
{
    Queue<TreeNode> queue = new Queue<TreeNode>();
    queue.Enqueue(node);
    TreeNode treeNode = null;
    while(queue.Count>0)
    {
        treeNode = queue.Dequeue();
        if(treeNode.LeftChild!=null)
            queue.Enqueue(treeNode.LeftChild);
        if(treeNode.RightChild!=null)
            queue.Enqueue(treeNode.RightChild);
        Console.WriteLine(treeNode.data);
    }     
}   

  

层序遍历