树(Tree, Uva 548)

Tree, Uva 548

You are to determine the value of the leaf node in a given binary tree that is the terminal node of a path of least value from the root of the binary tree to any leaf. The value of a path is the sum of values of nodes along that path.

Input

The input file will contain a description of the binary tree given as the inorder and postorder traversal sequences of that tree. Your program will read two line (until end of file) from the input file. The first line will contain the sequence of values associated with an inorder traversal of the tree and the second line will contain the sequence of values associated with a postorder traversal of the tree. All values will be different, greater than zero and less than 10000. You may assume that no binary tree will have more than 10000 nodes or less than 1 node.

Output

For each tree description you should output the value of the leaf node of a path of least value. In the case of multiple paths of least value you should pick the one with the least value on the terminal node.

Sample Input

3 2 1 4 5 7 6
3 1 2 5 6 7 4
7 8 11 3 5 16 12 18
8 3 11 7 16 18 12 5
255
255

Sample Output

1
3
255

分析
后序遍历的第一个字符就是根结点,因此只需在中序遍历中找到它,就知道左右子树的中序和后序遍历了。
这样可以先把二叉树构造出来,然后再执行一次递归遍历,找到最优解。

public class TreeDFS {

    static int maxv = 10000 + 10;
    static int[] inOrder;// 中序遍历序列
    static int[] postOrder;// 后序遍历序列
    static int[] lch;// 对应结点的左子节点
    static int[] rch;// 对应结点的右子节点
    static int best;// 最优解
    static int bestSum;// 目前为止最优解对应的权和

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            String[] line = sc.nextLine().split(" ");
            inOrder = readNode(line);// 中序遍历
            line = sc.nextLine().split(" ");
            postOrder = readNode(line);// 后序遍历
            lch = new int[maxv];
            rch = new int[maxv];
            int root = build(0, inOrder.length - 1, 0, postOrder.length - 1);
            bestSum = 1000000000;
            dfs(root, 0);
            System.out.println(best);

        }
        sc.close();
    }

    private static void dfs(int u, int sum) {
        // TODO 查找权值最优解 ---- u 当前结点的权值
        sum += u;
        if (lch[u] == 0 && rch[u] == 0) {// 当某结点没有左右子结点,则该结点为叶子
            if (sum < bestSum || (sum == bestSum && u < best)) {
                best = u;
                bestSum = sum;
            }
        }
        if (lch[u] != 0) {
            dfs(lch[u], sum);
        }
        if (rch[u] != 0) {
            dfs(rch[u], sum);
        }
    }

    private static int build(int L1, int R1, int L2, int R2) {
        // TODO 建立二叉树,返回树的根结点的值
        if (L1 > R1) {
            return 0;// 空树
        }
        int root = postOrder[R2];
        int p = L1;
        while (inOrder[p] != root) {
            p++;
        }
        int cnt = p - L1;// 左子树的结点个数
        lch[root] = build(L1, p - 1, L2, L2 + cnt - 1);
        rch[root] = build(p + 1, R1, L2 + cnt, R2 - 1);
        return root;
    }

    private static int[] readNode(String[] line) {
        // TODO 读取遍历序列
        int[] order = new int[line.length];
        for (int i = 0; i < order.length; i++) {
            int nodeVal = Integer.parseInt(line[i]);
            order[i] = nodeVal;
        }
        return order;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您好!要在 ECharts 中实现Tree)的点击节点功能,您可以按照以下步骤进行操作: 1. 首先,确保您已经引入了 ECharts 库,并在页面中创建了一个具有指定 ID 的容器,用于渲染图表。 2. 创建一个包含节点数据的 JSON 对象。每个节点应包含 `name` (节点名称)和 `children`(子节点)属性。 3. 使用 ECharts 的 `option` 对象配置项,设置图的基本配置,例如 `series` 类型为 `'tree'`,并指定 `data` 属性为您的节点数据。 4. 在 `series` 配置项中,使用 `expandAndCollapse` 属性来启用节点的展开和折叠功能。 5. 在 `series` 配置项中,使用 `label` 属性来设置节点的文本样式。 6. 在 `series` 配置项中,使用 `emphasis` 属性来设置节点的高亮样式。 7. 使用 ECharts 的 `on` 方法,监听 `'click'` 事件,并在回调函数中处理节点的点击事件。 下面是一个示例代码,演示了如何实现节点的点击功能: ```javascript // 基于准备好的dom,初始化echarts实例 var myChart = echarts.init(document.getElementById('chart-container')); // 节点数据 var treeData = { name: 'Root', children: [ { name: 'Node 1', children: [ { name: 'Leaf 1-1' }, { name: 'Leaf 1-2' } ] }, { name: 'Node 2', children: [ { name: 'Leaf 2-1' }, { name: 'Leaf 2-2' } ] } ] }; // 配置项 var option = { series: [ { type: 'tree', data: [treeData], expandAndCollapse: true, label: { show: true, position: 'top', formatter: '{b}' }, emphasis: { focus: 'descendant' } } ] }; // 渲染图表 myChart.setOption(option); // 监听节点点击事件 myChart.on('click', function(params) { console.log('点击了节点:', params); // 根据需要执行相应的操作 }); ``` 请注意,这只是一个简单的示例,您可以根据实际需求进行更复杂的配置和处理。希望对您有所帮助!如果有任何疑问,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值