[华为OD] B卷 树状结构查询 200

题目:

通常使用多行的节点、父节点表示一棵树,比如

西安 陕西

陕西 中国

江西 中国

中国 亚洲

泰国 亚洲

输入一个节点之后,请打印出来树中他的所有下层节点

输入描述

第一行输入行数,下面是多行数据,每行以空格区分节点和父节点

接着是查询节点

输出描述

输出查询节点的所有下层节点。以字典序排序Q

备注

树中的节点是唯一的,不会出现两个节点,是同一个名字

示例1:

输入

5

b a

c a

d c

e c

f d

c

输出

d

e

f

题解:

题目不难,就是构建一个树状的数据结构,只是遍历打印的时候需要注意下,如果子节点有多重树的话,先打印子节点,再打印子节点的子节点。

代码:

java 复制代码
import java.util.*;

public class TreeInfo {
    public static void main(String[] args) {

        Map<String, Tree> treeMap = new HashMap<>();
        Scanner sc = new Scanner(System.in);

        int lines = Integer.valueOf(sc.nextLine());
        for (int i = 0; i < lines; i++) {
            String[] treesInfo = sc.nextLine().split(" ");
            Tree tree = new Tree(treesInfo[1]);
            String child = treesInfo[0];
            if (treeMap.containsKey(tree.getParent())) {
                tree = treeMap.get(treesInfo[1]);
                tree.allChildRen(child);
            } else {
                tree.allChildRen(child);
            }
            treeMap.put(treesInfo[1], tree);
        }

        String treeNode = sc.nextLine();
        List<String> nextNodes = new ArrayList<>();

        printChild(treeNode, treeMap, nextNodes);
    }

    private static void printChild(String node, Map<String, Tree> treeMap, List<String> nextNodes) {
        List<String> children = treeMap.get(node).getChildren();
        for (String child : children) {
            System.out.println(child);
            if (treeMap.containsKey(child)) {
                nextNodes.add(child);
            }
        }
        if (nextNodes.size() > 0) {
            String newNode = nextNodes.get(0);
            nextNodes.remove(0);
            printChild(newNode, treeMap, nextNodes);
        }
    }

    private static class Tree {
        String parent;
        List<String> children;

        public Tree(String parent) {
            this.parent = parent;
            children = new ArrayList<>();
        }

        public String getParent() {
            return parent;
        }

        public void setParent(String parent) {
            this.parent = parent;
        }

        public List<String> getChildren() {
            return children;
        }

        public void setChildren(List<String> children) {
            this.children = children;
        }

        private void allChildRen(String child) {
            if (this.children != null && this.children.contains(child)) {
                return;
            } else {
                this.children.add(child);
            }
        }
    }
}

验证:

相关推荐
大数据张老师17 小时前
数据结构——直接插入排序
数据结构·算法·排序算法·1024程序员节
给大佬递杯卡布奇诺19 小时前
FFmpeg 基本数据结构 AVPacket分析
数据结构·c++·ffmpeg·音视频
南方的狮子先生19 小时前
【数据结构】从线性表到排序算法详解
开发语言·数据结构·c++·算法·排序算法·1024程序员节
极客智造19 小时前
编程世界的内在逻辑:深入探索数据结构、算法复杂度与抽象数据类型
数据结构·算法·数学建模
ゞ 正在缓冲99%…20 小时前
leetcode375.猜数字大小II
数据结构·算法·leetcode·动态规划
水蓝烟雨1 天前
0430. 扁平化多级双向链表
数据结构·链表
阿巴~阿巴~1 天前
Linux线程与进程的栈管理、页表机制及线程封装
数据结构·线程·进程·线程封装·页表机制·栈管理
立志成为大牛的小牛1 天前
数据结构——三十一、最小生成树(王道408)
数据结构·学习·程序人生·考研·算法
JMzz1 天前
Rust 中的数据结构选择与性能影响:从算法复杂度到硬件特性 [特殊字符]
开发语言·数据结构·后端·算法·性能优化·rust
星空露珠1 天前
数独生成题目lua脚本
数据结构·数据库·算法·游戏·lua