Data Structure Binary Tree: Diameter of a Binary Tree
http://www.geeksforgeeks.org/diameter-of-a-binary-tree/
#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);
} int diameter(node *root, int &ans) {
if (!root) return ;
int l = diameter(root->left, ans);
int r = diameter(root->right, ans);
ans = max(ans, l+r+);
return max(l, r) + ;
} 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();
int ans = ;
diameter(root, ans);
cout << ans << endl;
return ;
}
Data Structure Binary Tree: Diameter of a Binary Tree的更多相关文章
- 【转】Senior Data Structure · 浅谈线段树(Segment Tree)
本文章转自洛谷 原作者: _皎月半洒花 一.简介线段树 ps: _此处以询问区间和为例.实际上线段树可以处理很多符合结合律的操作.(比如说加法,a[1]+a[2]+a[3]+a[4]=(a[1]+a[ ...
- [Data Structure] Tree - relative
Segment Tree First, try to build the segment tree. lintcode suggest code: Currently recursion recomm ...
- LEETCODE —— binary tree [Same Tree] && [Maximum Depth of Binary Tree]
Same Tree Given two binary trees, write a function to check if they are equal or not. Two binary tre ...
- 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 ...
- 遍历二叉树 traversing binary tree 线索二叉树 threaded binary tree 线索链表 线索化
遍历二叉树 traversing binary tree 线索二叉树 threaded binary tree 线索链表 线索化 1. 二叉树3个基本单元组成:根节点.左子树.右子树 以L.D.R ...
- 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)空间复杂度:字典树远远小于哈 ...
- Week2 - 669. Trim a Binary Search Tree & 617. Merge Two Binary Trees
Week2 - 669. Trim a Binary Search Tree & 617. Merge Two Binary Trees 669.Trim a Binary Search Tr ...
随机推荐
- 在多线程的情况下是由Iterator遍历修改集合对象,报ConcurrentModificationException()异常的根因分析
遍历List时抛ConcurrentModificationException异常原理分析 http://www.blogjava.net/houlinyan/archive/2008/04/ ...
- LBS 附近的人
1 http://www.infoq.com/cn/articles/depth-study-of-Symfony2 2 http://lbsyun.baidu.com/
- linux内核参数分析
这是原来在51cto写的,现在都转到博客园来了 1.time_wait连接数过多,需要调整内核参数 http://blog.renhao.org/2010/07/setup-linux-kernel- ...
- Squid 启动/停止/重载配置文件 命令
当你的 squid.conf 配置文档按照你的想法修改完以后,启动 squid 之旅就开始了. Squid安装设试命令: 1,初始化你在 squid.conf 里配置的 cache 目录 #/usr/ ...
- Android SDK环境搭建
方法有二 方法一: Android SDK开发包国内下载地址 http://www.cnblogs.com/bjzhanghao/archive/2012/11/14/android-platform ...
- iOS swift NSClassFromString将字符串转换成类名
在oc中将字符串转换成类名直接调用NSClassFromString("classname")即可,但是到了swift中变的麻烦多了 swift中如果要将字符串转换为类型需要以下几 ...
- 下载并导出数据到execl中
下载poi-3.6-20091214.jar.下载地址例如以下: http://download.csdn.net/detail/evangel_z/3895051 1.jsp <button ...
- linux kernel学习笔记-5内存管理_转
void * kmalloc(size_t size, gfp_t gfp_mask); kmalloc()第一个参数是要分配的块的大小,第一个参数为分配标志,用于控制kmalloc()的行为. km ...
- Ocelot --API网关简单使用
最近几个月一直在忙一个项目(感觉像是与世隔绝了),本来想好的是要写一些asp.net core 的一些简单使用上东西,也就放下了. 好在忙完了,也就重新开始写吧.写点什么呢?网上也有不少dotnet ...
- linux 上拷贝文件到windows 上 文件出现锁的文件
要在linux上拷贝出文件到windows上,那么文件必须是777的最高权限. chmod wb_redis -R