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. jQuery表单 Ajax向PHP服务端发送文件请求并返回数据

    ImageAjaxUpLoad.htm <!DOCTYPE html> <head> <meta charset='utf-8'> <title>< ...

  2. Android 下Service

    1 http://www.cnblogs.com/newcj/archive/2011/05/30/2061370.html 2 http://blog.csdn.net/android_tutor/ ...

  3. Android下缓存以及解决OOM问题

    1 http://my.oschina.net/ryanhoo/blog/93285 开篇来个简单的后续的慢慢补充 2 http://www.apkbus.com/forum.php?mod=view ...

  4. 【C语言疯狂讲义】(三)C语言运算符

    1.运算符: 连接两个操作数(常量.变量)的符号 用运算符依照一定的规则连接的式子称为表达式 运算符的分类: 1)操作数的个数: 单目运算(++     sizeof    !) 双目运算符:... ...

  5. A股市场底部顶部历史数据

    1. A股市场平均市盈率 大顶沪市平均市盈率:66-70倍. A股市场2次大底沪市平均市盈率:12倍-15倍. 大底时的例子. 2005年6月6日上证指数1000点时的14倍市盈率.2008年10月2 ...

  6. tomcat启动之后报404

    启动之后什么异常都没有,但是就报404,很伤,为此和女朋友分了手. 如果项目以前还是可以正常运行的话,不妨试下下面这个办法: 停止tomcat,把tomcat下面的项目删除掉,之后右键单击项目,run ...

  7. Java中的equals方法和自定义比较器

    Object中的equals()方法默认是按地址比较,而不按内容进行比较, public boolean equals(Object obj) { return (this == obj); } 在S ...

  8. 关于工作与生活——HP大中华区总裁孙振耀撰文谈退休并畅谈人生

    转自:http://blog.csdn.net/adaptiver/article/details/7494121 我有个有趣的观察,外企公司多的是25-35岁的白领, 40岁以上的员工很少,二三十岁 ...

  9. linux 中vim学习与总结

    平常使用vim总是忘记快捷键,在这里做一个总结一下比较常用的快捷把,省的每次都要去查. h : 向左移动一个字符(←) j : 向上移动一个字符(↑) k : (↓) l : (→) ctrl+f : ...

  10. 调用http接口耗时过长。

    利用CRUL命令简单分析请求细节所占用的时间吧 curl -o /dev/null -s -w %{http_code}:%{time_namelookup}:%{time_redirect}:%{t ...