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 ...
随机推荐
- url中带有加号的处理方法
最近项目中出现了一个问题,图片的路径正确,但是转成URL之后无法找到... 找了各种原因之后,最后注意到URL中的图片名称和本地路径名称有点不一样,如下图 1.URL图片 2.本地路径 上网查了一下发 ...
- PHP面试题及答案解析(2)—PHP面向对象
1. 写出 php 的 public.protected.private 三种访问控制模式的区别. public:公有,任何地方都可以访问protected:继承,只能在本类或子类中访问,在其它地方不 ...
- 从两张Excel表所想到的
从两张Excel表所想到的 前几日,客服妹子发过来几张表,让我给她做下匹配,然后做了,想了,便有了这篇博文,不由感慨,看似简简单单的两张Excel表其实藏着好多东西,记叙如下,与君共勉. 最初的需求: ...
- [C++设计模式] singleton 单例模式
这个设计模式主要目的是想在整个系统中仅仅能出现一个类的实例.这样做当然是有必定的.比方你的软件的全局配置信息,或者是一个Factory,或是一个主控类,等等. 你希望这个类在整个系统中仅仅能出现一个实 ...
- android activity声明周期学习笔记
android生命周期图: Activity继承了ApplicationContext: 1:初次加载activity时顺序执行:onCreate()-->onStart()-->onRe ...
- POJ 1731 Orders(STL运用)
题目地址:POJ 1731 这题能够直接用STL函数做,非常轻松..next_permutation函数非常给力.. 代码例如以下: #include <algorithm> #inclu ...
- Unity2D实现人物三连击
之前写过一个系列<HTML5 2D平台游戏开发>,在此过程中发现有很多知识点没有掌握,而且用纯JavaScript来开发一个游戏效率极低,因为调试与地图编辑都没有可视化的工具,开发起来费时 ...
- oracle中位图索引和B-tree索引的区别
1.适用系统的不同:位图索引适合OLAP系统,而B-tree索引适合OLTP系统. 2.占用存储空间不同:位图索引只需要很小的存储空间,而B-tree索引需要占用很大的存储空间. 3.创建需要的时间不 ...
- Ionic项目打包安卓APK
之前用Ionic+Angular做了几个小应用Demo,现在用其中一个做实验试下打包安卓的APK安装包.(备注:我用的应用demo是之前博客里写的汇率的Demo,不清楚的同学可以查哈~) 我是用ion ...
- windows 10右键项添加Notepad++
1.打开注册表编辑器,开始->运行->regedit. 2.在HKEY_CLASSSES_ROOT→ * → Shell 下,在Shell下,新建项命名为Open With Notepad ...