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 ...
随机推荐
- ZooKeeper安装与执行
首先从官网下载ZooKeeper压缩包,然后解压下载得到的ZooKeeper压缩包,发现有"bin,conf,lib"等文件夹. "bin文件夹"中存放有执行脚 ...
- 如何为Apache JMeter开发插件(一)
本文转载于http://blog.csdn.net/column/details/12925.html,作者:xreztento 作者写的很精华,我打算在此系列操作一遍后,加多点截图,便于更多人更快上 ...
- jmeter压测-负载配置
jmeter 压测 一般压测的时间是10-15分钟 TPS:服务端每秒钟处理的请求数 越大越好 响应时间 :越短越好 并发用户数 也就是多少并发 指标给你:tps要达到多少 响应时间要达到多少 并 ...
- asp.net core mvc视频A:笔记3-1.视图基本用法
常用介绍 注意:ViewBag是对View的封装,所以如果两者键值(Key)是一样的话,后者会覆盖前者. 新建项目,添加空控制器 小技巧-快速添加视图 控制器方法,使用ViewData和ViewBag ...
- 面向对象-Object类
一.Object类中的equals()方法 equals(Object obj) :指示其它某个对象是否与此对象"相等". 返回值类型是boolean Oblect类中的equal ...
- javascript与as3交互
文章都是发布在github再转到这边的,这边格式可能会乱掉.博客地址:benqy.com 写在前面的废话 公司首页的flash广告,都是由第三方制作的,脚本和flash文件都是由各个广告公司独立制作, ...
- YUV图像合成原理
http://blog.csdn.net/zwz1984/article/details/50403150 http://zhongcong386.blog.163.com/blog/static/1 ...
- hdu 2349 最小生成树
/* 刚開始想错了,我以为必须是相邻的点才干连接.原来无线距离能够随意连接 对最小生成树理解不够深啊 */ #include<stdio.h> #include<math.h> ...
- python的requests初步使用
转自:http://my.oschina.net/yangyanxing/blog/280029 早就听说requests的库的强大,只是还没有接触,今天接触了一下,发现以前使用urllib,urll ...
- 机器学习2—K近邻算法学习笔记
Python3.6.3下修改代码中def classify0(inX,dataSet,labels,k)函数的classCount.iteritems()为classCount.items(),另外p ...