http://www.geeksforgeeks.org/convert-an-arbitrary-binary-tree-to-a-tree-that-holds-children-sum-property/

 #include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
using namespace std; struct node {
int data;
struct node *left, *right;
node() : data(), left(NULL), right(NULL) { }
node(int d) : data(d), left(NULL), right(NULL) { }
}; void print(node *node) {
if (!node) return;
print(node->left);
cout << node->data << " ";
print(node->right);
} void _convert(node* root, int diff) {
if (!root) return;
if (root->left) {
root->left->data += diff;
_convert(root->left, diff);
}
else if (root->right) {
root->right->data += diff;
_convert(root->right, diff);
}
} void convert(node* root) {
if (!root || !root->left && !root->right) return;
convert(root->left);
convert(root->right);
int sum = ;
sum += root->left? root->left->data : ;
sum += root->right? root->right->data : ;
if (sum >= root->data) root->data = sum;
else _convert(root, root->data - sum);
} int main() {
struct node* root = new node();
root->left = new node();
root->right = new node();
root->left->left = new node();
root->left->right = new node();
root->right->left = new node();
root->right->right = new node();
print(root);
cout << endl;
convert(root);
print(root);
return ;
}

Data Structure Binary Tree: Convert an arbitrary Binary Tree to a tree that holds Children Sum Property的更多相关文章

  1. Data Structure Binary Tree: Convert a given Binary Tree to Doubly Linked List

    http://www.geeksforgeeks.org/in-place-convert-a-given-binary-tree-to-doubly-linked-list/ #include &l ...

  2. Data Structure Binary Tree: Check for Children Sum Property in a Binary Tree

    http://www.geeksforgeeks.org/check-for-children-sum-property-in-a-binary-tree/ #include <iostream ...

  3. Finger Trees: A Simple General-purpose Data Structure

    http://staff.city.ac.uk/~ross/papers/FingerTree.html Summary We present 2-3 finger trees, a function ...

  4. Convert a given Binary Tree to Doubly Linked List

    The question and solution are from: http://www.geeksforgeeks.org/convert-given-binary-tree-doubly-li ...

  5. Convert string to binary and binary to string in C#

    String to binary method: public static string StringToBinary(string data) { StringBuilder sb = new S ...

  6. [Data Structure] Tree - relative

    Segment Tree First, try to build the segment tree. lintcode suggest code: Currently recursion recomm ...

  7. Binary Tree Level Order Traversal,Binary Tree Level Order Traversal II

    Binary Tree Level Order Traversal Total Accepted: 79463 Total Submissions: 259292 Difficulty: Easy G ...

  8. 遍历二叉树 traversing binary tree 线索二叉树 threaded binary tree 线索链表 线索化

    遍历二叉树   traversing binary tree 线索二叉树 threaded binary tree 线索链表 线索化 1. 二叉树3个基本单元组成:根节点.左子树.右子树 以L.D.R ...

  9. Python: tree data structure

    # 树结构 from pythonds.basic.stack import Stack #pip install pythonds from pythonds.trees.binaryTree im ...

随机推荐

  1. xpinyin-函数返回多个值-lambda匿名函数-列表生成式-三元表达式

    import xpinyinp=xpinyin.Pinyin() #实例化print(p.get_pinyin('小白','')) 函数返回多个值:1.函数如果返回多个值的话,它会把这几个值放到一个元 ...

  2. java学习笔记——java中对象的创建,初始化,引用的解析

    如果有一个A类. 1.例如以下表达式: A  a1 = new A(); 那么A是类,a1是引用.new A()是对象.仅仅是a1这个引用指向了new A()这个对象. 2.又如: A  a2; A代 ...

  3. Atitit.获取approot api 应用根路径 java c#.net php asp

    Atitit.获取approot api 应用根路径 java c#.net php asp 1. 如果根路径返回empty,否则返回/app,兼容getContextPath() <scrip ...

  4. 利用python 掌握机器学习的过程

    转载:http://python.jobbole.com/84326/ 偶然看到的这篇文章,觉得对我挺有引导作用的.特此跟大家分享一下. 为了理解和应用机器学习技术,你需要学习 Python 或者 R ...

  5. Ubuntu16.04下搜狗输入法、Sublime Text 3的安装

    Ubuntu16.04下搜狗输入法.Sublime Text 3的安装 一.搜狗输入法 1. 安装中文语言 默认在Ubuntu16.04下是没有中文的,需要安装中文,在System Settings- ...

  6. File 的基本操作

    package xinhuiji_day07; import java.io.File;import java.io.IOException; public class FileTest { /**  ...

  7. Weka学习之预处理连接MySql(二)

    载入数据 (一)打开文件 (二) 打开url (三) 打开数据库 (四)从一些数据生成器(DataGenerators)中生成人造数据    这篇主要写(三)中的连接mySql          网上 ...

  8. linux用一键安装包 禅道

    ----------------1.重启apache 和 mysql /opt/zbox/zbox restart -----------------2.问题1:发现端口被占用 apache启动报错( ...

  9. Hadoop环境搭建1_JDK+SSH

    1 前言: Hadoop 最早是为了在Linux 平台上使用而开发的,但是Hadoop 在UNIX.Windows 和Mac OS X 系统上也运行良好.不过,在Windows 上运行Hadoop 稍 ...

  10. Spring Cloud 微服务二:API网关spring cloud zuul

    前言:本章将继续上一章Spring Cloud微服务,本章主要内容是API 网关,相关代码将延续上一章,如需了解请参考:Spring Cloud 微服务一:Consul注册中心 Spring clou ...