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. MySQL - Show Processlist 整理(转)

      原文来源:MySQL 5.5 Reference Manual 部分翻译取自:<MySQL_5.1中文参考手册> 转载请注明原文链接http://www.cnblogs.com/len ...

  2. 用python做自己主动化測试--绘制系统性能趋势图和科学计算

    在性能測试中.我们常常须要画出CPU memory 或者IO的趋势图. 预计大学里.大多数人都学习过matlib, 领略了matlib绘图的强大. python提供了强大的绘图模块matplotlib ...

  3. web-压力测试学习

    https://blog.linuxeye.com/335.html http://bdql.iteye.com/blog/291987 http://www.cnblogs.com/zhuque/a ...

  4. oracle数据库权限管理

    权限管理: oracle 9里面默认的三个username和password: sys change_on_install //权限最高的管理员 system manager //普通的管理员 sco ...

  5. linux 内存分析

    http://blog.yufeng.info/archives/2456  这篇文章不错 值得看 http://www.361way.com/memory-analysis/5018.html

  6. [django] 利用多线程添加异步任务

    看到django异步大家的反应应该是celery这样的消息队列组件.如今用的最多的最推荐的也是这样的方式.然而我这需求就是请求来了,运行一个小程序.可是又不能确定这个小程序啥时候运行完.响应又要及时, ...

  7. Android OOM的解决方式

    尽量不要使用setImageBitmap或setImageResource或BitmapFactory.decodeResource来设置一张大图. 由于这些函数在完毕decode后,终于都是通过ja ...

  8. 一个可以模拟GET,POST,PUT,DELET请求的HTTP在线工具

    一个简陋的HTTP请求工具,UI比较丑陋.0.0,可以用于接口调试. 之前在调试公司的远程接口的时候用的是curl,后来也在网上找到几种Http请求模拟的客户端程序.当时后来发现google app ...

  9. Away3D引擎学习笔记(二)CameraController相机控制的应用

    cameraController---相机控制器 这里针对Away3D里面封装的CameraController的区别和使用做些简单介绍.相机控制器的设计思路比较清晰,所以难点东西不多.使用方面附上源 ...

  10. webpack 功能大全 【环境配置】

    1. webpack简介 webpack 是一个模块打包工具.它使得模块相互依赖并且可构建等价于这些模块的静态资源.相比于已经存在的模块打包器(module bundler),webpack的开发动机 ...