#include <cstdio>
#include <climits>
#include <cassert>
#include <iostream>
#include <algorithm>
#include <string> struct Node {
int value = ;
struct Node *left = nullptr;
struct Node *right = nullptr;
}; void preorder_print_aux(Node *T) {
if (T == nullptr) {
return ;
}
std::cout << T->value << ' ';
preorder_print_aux(T->left);
preorder_print_aux(T->right);
} void preorder_print(Node *T) {
preorder_print_aux(T);
std::cout << std::endl;
} Node *CreateBiTreeAux(Node *&T, std::vector<int>::const_iterator &ite) {
// 按先序次序输入二叉树中结点的值(一个字符),空格字符表示空树,
// 构造二叉链表表示的二叉树T。
int elem = *ite++; if (elem == INT_MIN) {
T = nullptr;
} else {
T = new Node;
T->value = elem; // 生成根结点
CreateBiTreeAux(T->left, ite); // 构造左子树
CreateBiTreeAux(T->right, ite); // 构造右子树
}
return T;
} Node *create_tree(Node *&T, const std::vector<int> &input) {
std::vector<int>::const_iterator ite = input.begin();
CreateBiTreeAux(T, ite); return T;
} void free(Node *&T)
{
if (T == nullptr)
return; free(T->left);
free(T->right);
delete T;
T = nullptr;
} int sum(struct Node *tree, int *max_sum) {
if (tree->left == nullptr && tree->right == nullptr) {
*max_sum = tree->value;
return tree->value;
} int left_sum = INT_MIN;
int l_max_sum = INT_MIN;
if (nullptr != tree->left) {
left_sum = sum(tree->left, &l_max_sum);
} int right_sum = INT_MIN;
int r_max_sum = INT_MIN;
if (nullptr != tree->right) {
right_sum = sum(tree->right, &r_max_sum);
} int ret = tree->value + left_sum + right_sum;
if (ret < l_max_sum) {
ret = l_max_sum;
}
if (ret < r_max_sum) {
ret = r_max_sum;
} *max_sum = ret;
return ret;
} void assert_equal(int expect, std::vector<int> tree) {
Node *T = nullptr;
create_tree(T, tree);
preorder_print(T);
int ret = ;
sum(T, &ret);
if (expect != ret) {
fprintf(stderr, "Expect %d, but result is %d.\n", expect, ret);
exit();
}
free(T);
} int main() {
assert_equal(, {, INT_MIN, INT_MIN});
assert_equal(, {, , INT_MIN, INT_MIN, , INT_MIN, INT_MIN});
assert_equal(, {, -, INT_MIN, INT_MIN, , INT_MIN, INT_MIN});
assert_equal(, {-, , -, INT_MIN, INT_MIN, , INT_MIN, INT_MIN, , INT_MIN, INT_MIN});
assert_equal(, {-, -, -, INT_MIN, INT_MIN, , INT_MIN, INT_MIN, -, INT_MIN, INT_MIN}); std::cout << "OK\n"; return ;
}

二叉树基本操作C++的更多相关文章

  1. c++学习笔记—二叉树基本操作的实现

    用c++语言实现的二叉树基本操作,包括二叉树的创建.二叉树的遍历(包括前序.中序.后序递归和非递归算法).求二叉树高度,计数叶子节点数.计数度为1的节点数等基本操作. IDE:vs2013 具体实现代 ...

  2. c语言二叉树基本操作

    编译器为vs2013 #include "stdafx.h" #include<malloc.h> #include<stdlib.h> #define O ...

  3. 二叉树基本操作C代码

    #include<stdio.h> #include<malloc.h> #define LEN sizeof(struct ChainTree) struct ChainTr ...

  4. c++(排序二叉树线索化)

    前面我们谈到了排序二叉树,还没有熟悉的同学可以看一下这个,二叉树基本操作.二叉树插入.二叉树删除1.删除2.删除3.但是排序二叉树也不是没有缺点,比如说,如果我们想在排序二叉树中删除一段数据的节点怎么 ...

  5. 基于Java实现红黑树的基本操作

    首先,在阅读文章之前,我希望读者对二叉树有一定的了解,因为红黑树的本质就是一颗二叉树.所以本篇博客中不在将二叉树的增删查的基本操作了,需要了解的同学可以到我之前写的一篇关于二叉树基本操作的博客:htt ...

  6. Leetcode#106 Construct Binary Tree from Inorder and Postorder Traversal

    原题地址 二叉树基本操作 [       ]O[              ] [       ][              ]O 代码: TreeNode *restore(vector<i ...

  7. 07. Go 语言接口

    Go 语言接口 接口本身是调用方和实现方均需要遵守的一种协议,大家按照统一的方法命名参数类型和数量来协调逻辑处理的过程. Go 语言中使用组合实现对象特性的描述.对象的内部使用结构体内嵌组合对象应该具 ...

  8. cb23a_c++_标准模板库STL_set_multiset_关联容器

    cb23a_c++_标准模板库STL_set_multiset_关联容器 set(集)数据不能重复.multiset(多集)可以重复.操作数据速度快,数据自动排序.红黑树(数据结构)红黑树-二叉树基本 ...

  9. cb22a_c++_标准模板库_STL_map_multimap红黑树(数据结构)关联容器

    cb22a_c++_标准模板库_STL_map_multimap红黑树(数据结构)关联容器map(映射,key不能重复,一对一对的,value_type(1, "one")),mu ...

随机推荐

  1. Firefox 插件 FlashGot 创建 Axel 下载任务

    运行脚本: #!/bin/sh# FlashGot Command line arguments template: [URL] [COMMENT] [FOLDER]if [ $# = 3 ]; th ...

  2. php SimpleXML 例子

    $txt = GetRemoteText($url); if(strlen($txt) > 0) { $xml = simplexml_load_string($txt); //获取xml if ...

  3. 串口 COM口 TTL RS-232 RS-485 区别 释疑

    Point: 1.串口.COM口是指的物理接口形式(硬件).而TTL.RS-232.RS-485是指的电平标准(电信号). 2.接设备的时候,一般只接GND RX TX.不会接Vcc或者+3.3v的电 ...

  4. 剑指Offer:面试题32——从1到n整数中1出现的次数(java实现)

    问题描述: 输入一个整数n,求1到n这n个整数的十进制表示中1出现的次数.例如输入12,从1到12这些整数中包含1的数字有1,10,11,12,1一共出现了5次. 思路:(不考虑时间效率的解法,肯定不 ...

  5. 缩略信息是: sending message to a Handler on a dead thread 我是用IntentService时报的

    稍微纤细一点儿的信息是: Handler (android.os.Handler) {215ddea8} sending message to a Handler on a dead thread. ...

  6. hibernate中的cascade和inverse

    以Student和class为例,一个Student对应一个class,一个class对应多个Student. Student.hbm.xml <?xml version="1.0&q ...

  7. C#pdf 切割成图片

    引用 using Ghostscript.NET;using Ghostscript.NET.Rasterizer; 需要安装 exe文件 public static GhostscriptVersi ...

  8. 转:python dict按照value 排序

    我们知道Python的内置dictionary数据类型是无序的,通过key来获取对应的value.可是有时我们需要对dictionary中 的item进行排序输出,可能根据key,也可能根据value ...

  9. [转帖]FPGA开发工具汇总

    原帖:http://blog.chinaaet.com/yocan/p/5100017074 ----------------------------------------------------- ...

  10. springmvc和http404错误

    今天解决一个java中的springmvc的问题,所有配置都是对的,主页面也能打得开,唯独Controller层的方法打不开,一直报http404错误 package com.gold.control ...