华为od(D卷)二叉树计算

文章目录

题目描述

给出一个二叉树如下图所示:

java 复制代码
     6
    / \
   7   9
    \  /  
    -2 6  

请由该二叉树生成一个新的二叉树,它满足其树中的每个节点将包含原始树中的左子树和右子树的和。

java 复制代码
      20 (7-2+9+6)
     /   \
    -2    6
     \   /  
     0  0 

左子树表示该节点左侧叶子节点为根节点的一颗新树;右子树表示该节点右侧叶子节点为根节点的一颗新树

输入描述

2行整数,

第1行表示二叉树的中序遍历,

第2行表示二叉树的前序遍历,以空格分割

例如:

7 -2 6 6 9

6 7 -2 9 6

输出描述

1行整数,表示求和树的中序遍历,以空格分割

例如:

输出1 -2 0 20 0 6

示例1

输入:

-3 12 6 8 9 -10 -7

8 12 -3 6 -10 9 -7
输出:

0 3 0 7 0 2 0

思路

1 . 前序中序构造二叉树

前序: 中左右; 判断"中"是第一个元素。

中序: 根据前序找到的"中" ,判断左右子树是谁。(此时可以提前计算左右子树的和)

代码

java 复制代码
public class Demo11 {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        // 中序
        int[] in = Arrays.stream(scanner.nextLine().split(" ")).mapToInt(Integer::parseInt).toArray();
        // 前序
        int[] pre = Arrays.stream(scanner.nextLine().split(" ")).mapToInt(Integer::parseInt).toArray();
        // 最终中序结果
        int[] resMid = new int[in.length];
        buildTree(pre, in, resMid, 0, pre.length, 0, in.length);
        System.out.println(Arrays.toString(resMid));
        scanner.close();

    }

    /**
     * @param pre      前序数组
     * @param in       中序数组
     * @param resMid   最终输出中序结果
     * @param preStart 前序开始索引
     * @param preEnd   前序结束索引
     * @param inStart  中序开始索引
     * @param inEnd    中序结束索引
     */
    public static void buildTree(int[] pre, int[] in, int[] resMid, int preStart, int preEnd, int inStart, int inEnd) {

        if (preStart == preEnd || inStart == inEnd) {
            return;
        }
        if (preEnd - preStart == 1 && inEnd - inStart == 1) {
            return;
        }
        
        // 中  为第一个元素
        int rootValue = pre[preStart];

        // 中  在中序中的位置
        int index = 0;
        for (int i = inStart; i < inEnd; i++) {
            if (in[i] == rootValue) {
                index = i;
                break;
            }
        }
        
        // 中序数组 左子树
        int inLeftStart = inStart;
        int inLeftEnd = index;
        // 中序数组的右子树
        int inRightStart = index + 1;
        int inRightEnd = inEnd;

        // 前序数组的  左子树
        int preLeftStart = preStart + 1;
        int preLeftEnd = preLeftStart + (index - inStart);
        // 前序数组的 右子树
        int preRightStart = preLeftEnd;
        int preRightEnd = preEnd;

        // 计算左右子树的和
        int[] inLeft = Arrays.copyOfRange(in, inLeftStart, inLeftEnd);
        int[] inRight = Arrays.copyOfRange(in, inRightStart, inRightEnd);
        resMid[index] = Arrays.stream(inLeft).sum() +
                Arrays.stream(inRight).sum();

        // 递归
        buildTree(pre, in, resMid, preLeftStart, preLeftEnd, inLeftStart, inLeftEnd);
        buildTree(pre, in, resMid, preRightStart, preRightEnd, inRightStart, inRightEnd);
    }
}
相关推荐
辰烨chenye2 小时前
LeetCode Hot 100 题解 · 哈希篇
算法·leetcode·哈希算法
罗西的思考2 小时前
DreamZero 与 DreamDojo:世界模型与策略的分层协同综合分析与对比
人工智能·算法·机器学习
玖玥拾3 小时前
LeetCode 219 存在重复元素 II
算法·leetcode·哈希算法·散列表
知无不研4 小时前
c语言中循环的介绍与简单应用
c语言·开发语言·算法·循环·for·while
a187927218315 小时前
【算法】动态规划第四篇:背包收官——min 哨兵、计数世界与组合排列分水岭
算法·leetcode·动态规划·dp·01背包·算法讲解·决策合并
2601_962297485 小时前
在python3中、下列输出变量a的正确写法是_2020超星大数据Python免费答案
数据结构·python·算法·编程·字符串操作
辰烨chenye6 小时前
LeetCode Hot 100 题解 · 子串篇
算法·leetcode·职场和发展
辰烨chenye6 小时前
LeetCode Hot 100 题解 · 堆篇
java·算法·leetcode
手写码匠6 小时前
华为云Flexus+DeepSeek征文|DeepSeek 应用监控告警体系实战:从“用户投诉才知道“到“故障前就发现“
人工智能·深度学习·算法·aigc