题目描述

给定一颗二叉树的逻辑结构(先序遍历的结果,空树用字符‘0’表示,例如AB0C00D00),建立该二叉树的二叉链式存储结构

二叉树的每个结点都有一个权值,从根结点到每个叶子结点将形成一条路径,每条路径的权值等于路径上所有结点的权值和。编程求出二叉树的最大路径权值。如下图所示,共有4个叶子即有4条路径,

路径1权值=5 + 4 + 11 + 7 = 27          路径2权值=5 + 4 + 11 + 2 = 22

路径3权值=5 + 8 + 13 = 26                路径4权值=5 + 8 + 4 + 1 = 18

可计算出最大路径权值是27。

该树输入的先序遍历结果为ABCD00E000FG00H0I00,各结点权值为:

A-5,B-4,C-11,D-7,E-2,F-8,G-13,H-4,I-1

输入

第一行输入一个整数t,表示有t个测试数据

第二行输入一棵二叉树的先序遍历,每个结点用字母表示

第三行先输入n表示二叉树的结点数量,然后输入每个结点的权值,权值顺序与前面结点输入顺序对应

以此类推输入下一棵二叉树

输出

每行输出每棵二叉树的最大路径权值,如果最大路径权值有重复,只输出1个

样例输入

2
AB0C00D00
4 5 3 2 6
ABCD00E000FG00H0I00
9 5 4 11 7 2 8 13 4 1

样例输出

11

27

这里只需给每个树节点添加属性weight即可,在创建树的时候每次传入父节点的weight,孩子节点迭代相加即可,并在类中设置属性maxleaveweight来记录叶子节点的最大权值,在创建树的时候就可以判断了,不需要再次调用任何一种遍历来设置maxleaveweigth

#include<iostream>
#include<string>
using namespace std;
class BitreeNode
{
public:
char data;
int weight;
int hight;
BitreeNode *left;
BitreeNode *right;
BitreeNode() :hight(),weight(),left(NULL), right(NULL) {}
~BitreeNode() {}
};
class Bitree
{
private:
BitreeNode *Root;
int pos,po;
string strtree;
BitreeNode *CreateBitree(int w[],int fatherweight);
void preorder(BitreeNode *t);
public:
int maxleaveweight;
Bitree() { maxleaveweight = ; };
~Bitree() {};
void CreateTree(string TreeArray,int w[]);
void preorder();
};
void Bitree::CreateTree(string treearray,int w[])
{
pos = ;
po = ;
strtree.assign(treearray);
Root = CreateBitree(w,);
}
BitreeNode *Bitree::CreateBitree(int w[],int fatherweight)
{
BitreeNode *T;
char ch;
ch = strtree[pos++];
if (ch == '')
T = NULL;
else
{
T = new BitreeNode();
T->data = ch;
T->weight = w[po++]+fatherweight;
if (!T->left && !T->right)
if (T->weight > maxleaveweight)
maxleaveweight = T->weight;
T->left = CreateBitree(w,T->weight);
T->right = CreateBitree(w,T->weight);
}
return T;
}
void Bitree::preorder()
{
preorder(Root);
cout << maxleaveweight << endl;
}
void Bitree::preorder(BitreeNode *t)
{
if (t)
{
if (!t->left && !t->right)
if (t->weight > maxleaveweight)
maxleaveweight = t->weight;
preorder(t->left);
preorder(t->right);
}
}
int main()
{
int t;
cin >> t;
while (t--)
{
string str;
cin >> str;
Bitree *tree;
int n,*w;
cin >> n;
w = new int[n];
for (int i = ; i < n; i++)
cin >> w[i];
tree = new Bitree();
tree->CreateTree(str,w);
cout << tree->maxleaveweight << endl;
}
}

DS树+图综合练习--二叉树之最大路径的更多相关文章

  1. DS树+图综合练习--带权路径和

    题目描述 计算一棵二叉树的带权路径总和,即求赫夫曼树的带权路径和. 已知一棵二叉树的叶子权值,该二叉树的带权案路径和APL等于叶子权值乘于根节点到叶子的分支数,然后求总和.如下图中,叶子都用大写字母表 ...

  2. DS树+图综合练习--构建邻接表

    题目描述 已知一有向图,构建该图对应的邻接表.邻接表包含数组和单链表两种数据结构,其中每个数组元素也是单链表的头结点,数组元素包含两个属性,属性一是顶点编号info,属性二是指针域next指向与它相连 ...

  3. ACM程序设计选修课——Problem D: (ds:树)合并果子(最优二叉树赫夫曼算法)

    Problem D: (ds:树)合并果子 Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 80  Solved: 4 [Submit][Status][ ...

  4. 以AVL树为例理解二叉树的旋转(Rotate)操作

    树旋转是在二叉树中的一种子树调整操作, 每一次旋转并不影响对该二叉树进行中序遍历的结果. 树旋转通常应用于需要调整树的局部平衡性的场合. 树旋转包括两个不同的方式, 分别是左旋转和右旋转. 两种旋转呈 ...

  5. 哈夫曼树【最优二叉树】【Huffman】

    [转载]只为让价值共享,如有侵权敬请见谅! 一.哈夫曼树的概念和定义 什么是哈夫曼树? 让我们先举一个例子. 判定树:         在很多问题的处理过程中,需要进行大量的条件判断,这些判断结构的设 ...

  6. [LeetCode] Binary Tree Maximum Path Sum 求二叉树的最大路径和

    Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. ...

  7. [LeetCode] 124. Binary Tree Maximum Path Sum 求二叉树的最大路径和

    Given a non-empty binary tree, find the maximum path sum. For this problem, a path is defined as any ...

  8. 75.Binary Tree Maximum Path Sum(二叉树的最大路径和)

    Level:   Hard 题目描述: Given a non-empty binary tree, find the maximum path sum. For this problem, a pa ...

  9. 有向无环图(DAG)的最小路径覆盖(转)

    DAG的最小路径覆盖 定义:在一个有向图中,找出最少的路径,使得这些路径经过了所有的点. 最小路径覆盖分为最小不相交路径覆盖和最小可相交路径覆盖. 最小不相交路径覆盖:每一条路径经过的顶点各不相同.如 ...

随机推荐

  1. Jaxb如何优雅的处理CData

    前言   Jaxb确实是xml和java对象映射互转的一大利器. 但是在处理CData内容块的时候, 还是有些小坑. 结合网上搜索的资料, 本文提供了一种解决的思路, 看看能否优雅地解决CData产出 ...

  2. [LeetCode&Python] Problem 13. Roman to Integer

    Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 ...

  3. python 中os的常用方法

    1.更改当前的路径 import os os.chdir( "D:/java") 注意python中表示文件路径,文件夹之间用/或者\\不能使用\

  4. CF449 (Div. 1简单题解)

    A .Jzzhu and Chocolate pro:现在给定一个大小为N*M的巧克力,让你横着或者竖着切K刀,都是切的整数大小,而且不能切在相同的地方,求最大化其中最小的块. (N,M,K<1 ...

  5. 洛谷P4147 玉蟾宫(动规:最大子矩形问题/悬线法)

    题目链接:传送门 题目大意: 求由F构成的最大子矩阵的面积.输出面积的三倍. 1 ≤ N,M ≤ 1000. 思路: 悬线法模板题. #include <bits/stdc++.h> us ...

  6. iptables filter表 案例、iptables nat表的路由功能 、端口映射

    1.小案例 #!/bin/bashipt="/usr/sbin/iptables"$ipt -F$ipt -P INPUT DROP$ipt -P OUTPUT ACCEPT$ip ...

  7. centos6下安装opencv3

    环境 centos6.5 opencv3.3.0 python3.4.9 下载 opencv可从GitHub下载https://github.com/opencv/opencv/tree/3.4.3可 ...

  8. java依赖注入(injection)

    和SpringSource分别通过其开源项目Guice及Spring Framework提供了依赖注入的功能.然而直到现在开发者也没有一种标准的.独立于供应商的方式从而无需修改其源文件就能在这些框架之 ...

  9. python str使用笔记(更新)

    判断字符串是否以某个串为结尾: str.endswith(strtmp) 返回True/False >>> strs='aba' >>> strs.endswith ...

  10. LeetCode - Number of Recent Calls

    Write a class RecentCounter to count recent requests. It has only one method: ping(int t), where t r ...