#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. 规则引擎集成接口(八)Java接口实例

    接口实例 右键点击“对象库” —“添加接口实例”,如下图: 弹出如下窗体: 输入接口的参数信息: 点击接口“求和”,选择选项卡“求和操作”,点击添加图标   ,如下: 弹出如下窗体,勾选方法“coun ...

  2. iOS-多线程 ,整理集锦,多种线程的创建

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launc ...

  3. mac boot2docker certs not valid with 1.7

    摘自:https://github.com/boot2docker/boot2docker/issues/824 An error occurred trying to connect: Get ht ...

  4. Func<T>、Action<T> 的区别于说明

    一.Func Func是一个.Net内置的委托. Func<Result>,Func<T1,Result>是一个.Net内置的泛型委托. Func<TResult> ...

  5. centos 7 挂载大硬盘

    对硬盘sdb进行分区 parted -a optimal /dev/sdb 使用GPT格式 mklabel gpt 建立一个主分区 mkpart primary - 显示分区信息 print 退出 q ...

  6. 在osx下通过vmware无GUI方式运行centos 7

    启动虚拟机: /Applications/VMware\ Fusion.app/Contents/Library/vmrun -T fusion start "CentOS 64-bit.v ...

  7. python模块与包的导入

    1. 模块与包的区别 模块,即module,一个包含python语句的.py文件就是一个模块!每个源代码文件都会自动成为模块!没有额外的语法用来声明模块. 包,又称模块包,即module packag ...

  8. Maven多环境打包

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...

  9. selenium+phantomjs爬取动态页面数据

    1.安装selenium pip/pip3 install selenium 注意依赖关系 2.phantomjs for windows 下载地址:http://phantomjs.org/down ...

  10. Leetcode4:Median of Two Sorted Arrays@Python

    There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...