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. Nginx 限制单个IP的并发连接数及对每个连接速度(限速)

    使用Nginx限制单个IP的并发连接数能够减少一些采集程序或者DDOS的攻击. 再lnmp的nginx配置中已经添加了部分代码,但是是注释掉的,可以编辑/usr/local/nginx/conf/ng ...

  2. 让UITableView进入编辑模式

    1.UITableView对象有一个editing属性,设为YES时,该对象会进入编辑模式(editing mode).表格视图进入编辑模式后,用户可以管理表格中得行,如改变行的排列顺序.增加行或删除 ...

  3. 企业级分布式存储应用与实战MogileFS、FastDFS

    项目实战9—企业级分布式存储应用与实战MogileFS.FastDFS   目录 实战一:企业级分布式存储应用与实战 mogilefs 实现 原理 1.环境准备 2.下载安装,每个机器都一样 3.数据 ...

  4. 在Linux平台使用VNC连接树莓派

    你的Linux发行版本号可能已经包括了Remote Desktop Viewer(远程桌面)程序.你能够通过这个程序使用VNC连接你的树莓派.通常能够在Applications/Internet菜单以 ...

  5. JavaScript的toString()

    JavaScript toString() 方法 JavaScript Boolean 对象 定义和用法 toString() 方法可把一个逻辑值转换为字符串,并返回结果. 语法 booleanObj ...

  6. ApplicationMaster是如何启动container并通信

    ApplicationMaster是如何启动container并通信 hadoop的关键进程 http://blog.csdn.net/jediael_lu/article/details/46386 ...

  7. 在Windows 10中开启开发者模式

    及以上)的电脑上使用Visual Studio来开发Windows 10或者Windows 8.1的应用,你可能会遇到下面的问题,要求你开启开发者模式. 于是你跑到设置里面,把开发者模式打开: 结果你 ...

  8. Effective C++ 49,50

    49.熟悉标准库. C++标准库非常大. 首先标准库中函数非常多,为了避免名字冲突.使用命名空间std.而之前的库函数都存放于< .h>中,如今成为伪标准库.而不能直接将这些头文件所有直接 ...

  9. java 性能检测工具 检测死锁等

    死锁检测方法 1 JConsole 找到需要查看的进程,打开线程选项卡,点击检测死锁 2 jps查看java进程ID,使用jstack  7412输出信息 3 使用jvisualvm连接java虚拟机 ...

  10. 自定义验证----required属性

    1,required属性 - 表示字段不能为空(注意:只有用户单击“提交”按钮提交表单的时候,浏览器才会执行验证.目前HTML5不支持指定验证的时间,而且验证消息的样式和内容各个浏览器不大一样,不能修 ...