相关疑难解决方法(0)

广度优先遍历

我试图解决一个面试问题,但为此我必须逐级旅行二叉树.我设计的BinaryNode具有以下变量

private object data;
private BinaryNode left;
private BinaryNode right;
Run Code Online (Sandbox Code Playgroud)

有人可以帮我在BinarySearchTree类中编写BreadthFirstSearch方法吗?

更新:感谢大家的投入.所以这是面试问题."给定一个二叉搜索树,设计一个算法,创建每个深度的所有节点的链表(即,如果你有一个深度为D的树,你将有D个链表)".

这是我的方法,让我知道你的专家评论.

public List<LinkedList<BNode>> FindLevelLinkList(BNode root)
    {
        Queue<BNode> q = new Queue<BNode>();
        // List of all nodes starting from root.
        List<BNode> list = new List<BNode>();
        q.Enqueue(root);
        while (q.Count > 0)
        {
            BNode current = q.Dequeue();
            if (current == null)
                continue;
            q.Enqueue(current.Left);
            q.Enqueue(current.Right);
            list.Add(current);
        }

        // Add tree nodes of same depth into individual LinkedList. Then add all LinkedList into a List
        LinkedList<BNode> LL = new LinkedList<BNode>();
        List<LinkedList<BNode>> result = …
Run Code Online (Sandbox Code Playgroud)

.net c# algorithm data-structures

40
推荐指数
2
解决办法
4万
查看次数

标签 统计

.net ×1

algorithm ×1

c# ×1

data-structures ×1