Data Structure Binary Tree: Convert a given tree to its Sum Tree
http://www.geeksforgeeks.org/convert-a-given-tree-to-sum-tree/
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
#include <string>
#include <fstream>
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 prints(node *root) {
if (!root) return;
prints(root->left);
cout << root->data << " ";
prints(root->right);
} int tosum(node *root) {
if (!root) return ;
int old = root->data;
root->data = tosum(root->left) + tosum(root->right);
return root->data + old;
} int main() {
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();
tosum(root);
prints(root);
return ;
}
Data Structure Binary Tree: Convert a given tree to its Sum Tree的更多相关文章
- 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 ...
- Data Structure Binary Tree: Convert an arbitrary Binary Tree to a tree that holds Children Sum Property
http://www.geeksforgeeks.org/convert-an-arbitrary-binary-tree-to-a-tree-that-holds-children-sum-prop ...
- Data Structure Binary Search Tree: Find k-th smallest element in BST (Order Statistics in BST)
http://www.geeksforgeeks.org/find-k-th-smallest-element-in-bst-order-statistics-in-bst/ #include < ...
- Data Structure Binary Tree: Lowest Common Ancestor in a Binary Tree
http://www.geeksforgeeks.org/lowest-common-ancestor-binary-tree-set-1/ #include <iostream> #in ...
- Data Structure Binary Tree: Print ancestors of a given binary tree node without recursion
http://www.geeksforgeeks.org/print-ancestors-of-a-given-binary-tree-node-without-recursion/ #include ...
- Data Structure Binary Tree: Iterative Postorder Traversal
http://www.geeksforgeeks.org/iterative-postorder-traversal-using-stack/ #include <iostream> #i ...
- Data Structure Binary Tree: Largest Independent Set Problem
http://www.geeksforgeeks.org/largest-independent-set-problem/ #include <iostream> #include < ...
- Data Structure Binary Tree: Morris traversal for Preorder
http://www.geeksforgeeks.org/morris-traversal-for-preorder/ #include <iostream> #include <v ...
- Data Structure Binary Tree: Construct Full Binary Tree from given preorder and postorder traversals
http://www.geeksforgeeks.org/full-and-complete-binary-tree-from-given-preorder-and-postorder-travers ...
随机推荐
- JavaScript 获取文件名,后缀名
function getBaseName(str) { var segs = str.split('.'); if(segs.length > 1) segs.pop(); return seg ...
- CLR 完全介绍
From: http://msdn.microsoft.com/zh-cn/magazine/cc164193.aspx http://msdn.microsoft.com/en-us/magazin ...
- 动态添加ImageView 设置setPadding不起作用问题
imageView = new ImageView(NavigationActivity.this); imageView.setLayoutParams(new LayoutParams(12,12 ...
- SQL之经典语句
一.基础 1.说明:创建数据库 CREATE DATABASE database-name 2.说明:删除数据库 drop database dbname 3.说明:备份sql server --- ...
- 解密SVM系列(二):SVM的理论基础(转载)
解密SVM系列(二):SVM的理论基础 原文博主讲解地太好了 收藏下 解密SVM系列(三):SMO算法原理与实战求解 支持向量机通俗导论(理解SVM的三层境界) 上节我们探讨了关于拉格朗日乘 ...
- FFmpeg与libx264 x264接口对应关系源代码分析
源代码位于“libavcodec/libx264.c”中.正是有了这部分代码,使得FFmpeg可以调用libx264编码H.264视频. 从图中可以看出,libx264对应的AVCodec结构体ff ...
- [转]python 书籍推荐
原地址: http://python.jobbole.com/85620/ https://github.com/jobbole/awesome-python-books http://blog.cs ...
- 【Atheros】minstrel速率调整算法源码走读
先说几个辅助的宏,因为内核不支持浮点运算,当然还有实现需要,minstrel对很多浮点值做了缩放: /* scaled fraction values */ #define MINSTREL_SCAL ...
- Linux进程间通信(IPC)机制总览
Linux进程间通信 Ø 管道与消息队列 ü 匿名管道,命名管道 ü 消息队列 Ø 信号 ü 信号基础 ü 信号应用 Ø 锁与信号灯 ü 记录锁 ü 有名信号灯 ü 无名信号灯(基 ...
- gIt 常用 操作
git提交代码流程git status -- 查看当前仓库状态git add -- 添加到临时仓库git commit -m '注释' -- 添加到临时仓库git status -- 查看当前仓库 ...