You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java实现BFS算法遇节点兄弟节点获取问题,求解决方案

Java二叉树BFS实现:解决节点遍历问题

你的Node类里没有父节点引用,没法直接从当前节点获取它的兄弟节点。而且BFS的核心逻辑不是处理兄弟节点——兄弟节点属于同一层级,会在队列的后续循环中被依次处理,不需要单独去获取。BFS的正确逻辑是遍历当前节点的所有子节点,把它们加入队列等待后续处理。

修正后的完整BFS实现

下面是结合你的代码修改后的可运行BFS,包含结果收集的逻辑:

import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class BFSExample {
    public static List<Integer> bfs(Node root) {
        List<Integer> result = new LinkedList<>();
        if (root == null) {
            return result;
        }

        Queue<Node> queue = new LinkedList<>();
        queue.add(root);
        root.visited = true;

        while (!queue.isEmpty()) {
            Node temp = queue.poll();
            // 将当前节点数据加入结果列表
            result.add(temp.data);

            // 处理左子节点:存在且未访问则标记入队
            if (temp.left != null && !temp.left.visited) {
                temp.left.visited = true;
                queue.add(temp.left);
            }
            // 处理右子节点:同理
            if (temp.right != null && !temp.right.visited) {
                temp.right.visited = true;
                queue.add(temp.right);
            }
        }
        return result;
    }

    // 你的Node类(可按需补充getter/setter)
    static class Node {
        int data;
        Node left;
        Node right;
        boolean visited;

        Node(int data) {
            this.data = data;
            this.left = null;
            this.right = null;
            this.visited = false;
        }
    }

    public static void main(String[] args) {
        // 树初始化
        Node node1 = new Node(1);
        Node node7 = new Node(7);
        Node node9 = new Node(9);
        Node node8 = new Node(8);
        Node node2 = new Node(2);
        Node node3 = new Node(3);
        node1.left = node7;
        node1.right = node9;
        node7.right = node8;
        node9.right = node3;
        node9.left = node2;

        // 执行BFS并打印结果
        List<Integer> bfsResult = bfs(node1);
        System.out.println("BFS遍历结果:" + bfsResult); // 输出:[1,7,9,8,2,3]
    }
}

代码说明

  • 用List<Integer>收集遍历结果,满足你代码中“获取结果列表”的需求。
  • 每次从队列取出节点后,先记录其数据,再检查左、右子节点:只要子节点存在且未被访问,就标记为已访问并加入队列。
  • 队列会自动维护层级顺序:先处理根节点1,再处理它的子节点7、9,接着处理7的子节点8、9的子节点2和3,完全符合BFS的层级遍历逻辑。

若确实需要获取兄弟节点(非BFS必需)

如果业务场景需要获取兄弟节点,你需要修改Node类添加父节点引用,并实现获取兄弟节点的方法:

static class Node {
    int data;
    Node left;
    Node right;
    Node parent; // 添加父节点引用
    boolean visited;

    Node(int data) {
        this.data = data;
        this.left = null;
        this.right = null;
        this.parent = null;
        this.visited = false;
    }

    // 获取兄弟节点的方法
    public Node getSibling() {
        if (parent == null) {
            return null; // 根节点没有兄弟
        }
        return parent.left == this ? parent.right : parent.left;
    }
}

初始化树时要同步设置父节点:

node1.left = node7;
node7.parent = node1;
node1.right = node9;
node9.parent = node1;
node7.right = node8;
node8.parent = node7;
node9.right = node3;
node3.parent = node9;
node9.left = node2;
node2.parent = node9;

注意:这个逻辑和BFS本身无关,BFS不需要依赖兄弟节点就能完成完整遍历。

内容的提问来源于stack exchange,提问作者Sercan Noyan Germiyanoğlu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 19:25:27