Data Structure Binary Tree: Check if a given Binary Tree is SumTree
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的更多相关文章
- Python: tree data structure
# 树结构 from pythonds.basic.stack import Stack #pip install pythonds from pythonds.trees.binaryTree im ...
- [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 ...
- 字典树(查找树) leetcode 208. Implement Trie (Prefix Tree) 、211. Add and Search Word - Data structure design
字典树(查找树) 26个分支作用:检测字符串是否在这个字典里面插入.查找 字典树与哈希表的对比:时间复杂度:以字符来看:O(N).O(N) 以字符串来看:O(1).O(1)空间复杂度:字典树远远小于哈 ...
- 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 ...
- [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 ...
- 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 ...
- LeetCode 958. Check Completeness of a Binary Tree
原题链接在这里:https://leetcode.com/problems/check-completeness-of-a-binary-tree/ 题目: Given a binary tree, ...
- 【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 ...
- 【LeetCode】958. Check Completeness of a Binary Tree 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS DFS 日期 题目地址:https://le ...
随机推荐
- 如果你报createSQLQuery is not valid without active transaction,请看这里
原文:https://blog.csdn.net/yinjian520/article/details/8666695 很多时候我们使用hibernate的session时,都是让session在某一 ...
- ubuntu下设置静态ip
直接修改系统配置文件ubuntu的网络配置文件是:/etc/network/interfacesubuntu命令行修改网络配置方法前面auto eth?,让网卡开机自动挂载. 为网卡配置静态IP地址 ...
- angular中使用daterangepicker完全能用版
angular版本:angular5 先看效果图: 最新版是这样的: 附上插件的网址: http://www.daterangepicker.com/ 1 安装: daterangepicker依赖于 ...
- 新建WORD文档打开会出现转换文件对话框3步解决办法
1.选择“纯文本”格式打开word文件. ------------------------------------------------------------------------------ ...
- static 关键字的使用,静态和非静态类的区别
直接以一个例子说明: using System; using System.Collections.Generic; using System.Diagnostics; using System.IO ...
- Ubuntu Server 命令行下显示中文乱码(菱形)解决办法
如果Ubuntu Server在安装过程中,选择的是中文(很多新手都会在安装时选择中文,便于上手),这样在完成安装后,系统默认的语言将会是中文zh_CN.UTF- 8.但问题是我们安装的是服务器,只需 ...
- Memcached的LRU和缓存命中率
缓存命中率 命中:直接从缓存中读取到想要的数据. 未中:缓存中没有想要的数据,还需要到数据库进行一次查询才能读取到想要的数据. 命中率越高,数据库查询的次数就越少. 读取缓存的速度远比数据库查询的速度 ...
- MyBatis_传入参数的问题
一.单个参数 1.基本数据类型 (1)直接使用 List<ChargeRuleDO> tests(long id); <select id="tests" res ...
- Error in as.POSIXlt.character(x, tz, ...) :
> sqlFetch(channel,"user")Error in as.POSIXlt.character(x, tz, ...) : character strin ...
- Hotel poj 3667
Language: Default Hotel Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 18020 Acc ...