http://www.geeksforgeeks.org/check-if-a-given-binary-tree-is-sumtree/

 #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) { }
}; bool isleaf(node *root) {
return !root->left && !root->right;
} bool issumtree(node *root) {
if (!root || isleaf(root)) return true;
int l = ;
int r = ;
if (root->left == NULL) l = ;
else if (isleaf(root->left)) l = root->left->data;
else l = * root->left->data;
if (root->right == NULL) r = ;
else if (isleaf(root->right)) r = root->right->data;
else r = * root->right->data;
return root->data == l + r && issumtree(root->left) && issumtree(root->right);
} 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->right = new node();
if (issumtree(root)) cout << "yes" << endl;
else cout << "NO" << endl;
return ;
}

Data Structure Binary Tree: Check if a given Binary Tree is SumTree的更多相关文章

  1. Python: tree data structure

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

  2. [Algorithms] Tree Data Structure in JavaScript

    In a tree, nodes have a single parent node and may have many children nodes. They never have more th ...

  3. 字典树(查找树) leetcode 208. Implement Trie (Prefix Tree) 、211. Add and Search Word - Data structure design

    字典树(查找树) 26个分支作用:检测字符串是否在这个字典里面插入.查找 字典树与哈希表的对比:时间复杂度:以字符来看:O(N).O(N) 以字符串来看:O(1).O(1)空间复杂度:字典树远远小于哈 ...

  4. LeetCode208 Implement Trie (Prefix Tree). LeetCode211 Add and Search Word - Data structure design

    字典树(Trie树相关) 208. Implement Trie (Prefix Tree) Implement a trie with insert, search, and startsWith  ...

  5. [Swift]LeetCode958. 二叉树的完全性检验 | Check Completeness of a Binary Tree

    Given a binary tree, determine if it is a complete binary tree. Definition of a complete binary tree ...

  6. 115th LeetCode Weekly Contest Check Completeness of a Binary Tree

    Given a binary tree, determine if it is a complete binary tree. Definition of a complete binary tree ...

  7. LeetCode 958. Check Completeness of a Binary Tree

    原题链接在这里:https://leetcode.com/problems/check-completeness-of-a-binary-tree/ 题目: Given a binary tree, ...

  8. 【leetcode】958. Check Completeness of a Binary Tree

    题目如下: Given a binary tree, determine if it is a complete binary tree. Definition of a complete binar ...

  9. 【LeetCode】958. Check Completeness of a Binary Tree 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS DFS 日期 题目地址:https://le ...

随机推荐

  1. 【转】.NET(C#):浅谈程序集清单资源和RESX资源 关于单元测试的思考--Asp.Net Core单元测试最佳实践 封装自己的dapper lambda扩展-设计篇 编写自己的dapper lambda扩展-使用篇 正确理解CAP定理 Quartz.NET的使用(附源码) 整理自己的.net工具库 GC的前世与今生 Visual Studio Package 插件开发之自动生

    [转].NET(C#):浅谈程序集清单资源和RESX资源   目录 程序集清单资源 RESX资源文件 使用ResourceReader和ResourceSet解析二进制资源文件 使用ResourceM ...

  2. Memcache针对不同场景数据应用缓存策略

    Memcache主要的作用是为减轻大访问量对数据库的冲击,所以一般的逻辑是首先从memcache中读取数据,如果没有就从数据库中读取数据写入到memcache中,等下一次读取的时候就可以从memcac ...

  3. [C/C++标准库]_[0基础]_[怎样实现std::string自己的Format(sprintf)函数]

    场景: 1.  C语言有自己的sprintf函数,可是这个函数有个缺点,就是不知道须要创建多大的buffer, 这时候能够使用snprintf函数来计算大小,仅仅要參数 buffer为NULL, co ...

  4. 每日一招:如何才能在A股持续盈利?

    证券市场介入门槛低,是众多普通投资者希望实现自己的“中国梦”的首选场所,无论牛市熊市,源源不断的参与者前赴后继.在沪深A股这样一个大众市场,每年都在上演着七亏两平一赚的悲喜剧,赚钱的永远是少数人,但众 ...

  5. sprint3 【每日scrum】 TD助手站立会议第九天

    站立会议 组员 昨天 今天 困难 签到 刘铸辉 (组长) 整合原来做过的功能,并做相应的改进,整合其他的功能 团队进入最终的功能测试阶段,准备发布Beta版 在测试阶段BUG太多,不知道如何解决 Y ...

  6. CoreAnimation的使用小结

    參考:http://www.cnblogs.com/wendingding/p/3801157.htmlhttp://www.cnblogs.com/wendingding/p/3802830.htm ...

  7. android开发游记:meterial design 5.0 开源控件整套合集 及使用demo

    android 的5.0公布不光google官方给出了一些新控件,同一时候还给出了一套符合material design风格的设计标准,这套标准将未来将覆盖google全部产品包括pc端,站点,移动端 ...

  8. homebrew可以管理众多开源软件的安装和卸载

    通过homebrew可以管理众多开源软件的安装和卸载. 参考https://github.com/mxcl/homebrew/wiki 1. 安装: ruby -e "$(curl -fsS ...

  9. vmware workstation导出ovf

    ovf tool路径 /Applications/VMware Fusion.app/Contents/Library/VMware OVF Tool 上面红色的部分,需要右键点击应用程序中的VMwa ...

  10. 录音整理文字工具otranscribe简介

    网址: http://otranscribe.com/ 首先载入音频文件,支持 mp3, ogg, webm, wav (HTML5 无需将文件上传至服务器,可保护隐私),然后就可以边听边整理了. 通 ...