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 ...
随机推荐
- Resharper快捷键及用法
建议你使用 Reshaper 的快捷键,不要担心 Reshaper 会把你原来的快捷键设置给覆盖了,因为如果某个快捷键和 VS 是冲突的,Reshaper会让你自己选择需要使用 VS 还是 Resha ...
- Linux基础之常用基本命令备忘
Linux基础之常用基本命令备忘 PWD 查询当前所在Linux上的位置 / 根目录 CD(change directory)切换目录 语法 CD /(注意添加空格) LS ...
- nodejs eclipse
nodejs下载地址 http://nodejs.org/1.下载并安装完nodejs后,打开cmd命令窗口,输入node -v,如果正确输出版本号,就是安装成功了,如果说node不是windows的 ...
- java多线程实现复制大文件
有些开发的时候我们经常遇到这样一个问题,对大文件的处理.比如:日志文件.那么十几G的大文件.我们应该如何复制呢? 还有就是希望从本地和远程复制文件,文件都很大,10G级的如何办呢? 在这里我告诉你们, ...
- cordova ios升级插件
org.ssgroup.sope.cordova.upgrade 支持强制升级与选择升级 插件已经开源在https://github.com/shenshouer/org.ssgroup.sope.c ...
- usermod命令
usermod 功能: 修改用户 常用参数:-c 账号说明-d 账号家目录-e 密码失效日期-g 主用户组GID-G 次用户组GID-l 账号名称-s she ...
- Spring Ioc (this is my first example)
一.首先看下源码结构 二.HelloWord 类 package com.northeasttycoon.bean; /** * 打印出 helloword 参数值 * * @author tycoo ...
- LCD驱动程序(二)
上节我们主要是对fb_info结构体的配置,对fb_info结构体的配置主要分为一下步骤: static int lcd_init(void){ /* 1. 分配一个fb_info */ s3c_lc ...
- 【BZOJ2115】[Wc2011] Xor 高斯消元求线性基+DFS
[BZOJ2115][Wc2011] Xor Description Input 第一行包含两个整数N和 M, 表示该无向图中点的数目与边的数目. 接下来M 行描述 M 条边,每行三个整数Si,Ti ...
- 【BZOJ4269】再见Xor 高斯消元
[BZOJ4269]再见Xor Description 给定N个数,你可以在这些数中任意选一些数出来,每个数可以选任意多次,试求出你能选出的数的异或和的最大值和严格次大值. Input 第一行一个正整 ...