首页 > 代码库 > the longest distance of a binary tree
the longest distance of a binary tree
the longest distance of a binary tree
个人信息:就读于燕大本科软件工程专业 目前大三;
本人博客:google搜索“cqs_2012”即可;
个人爱好:酷爱数据结构和算法,希望将来从事算法工作为人民作出自己的贡献;
博客内容:the longest distance of a binary tree;
博客时间:2014-4-15;
编程语言:C++ ;
编程坏境:Windows 7 专业版 x64;
编程工具:vs2008 32位编译器;
制图工具:office 2010 ppt;
硬件信息:7G-3 笔记本;
my words
simple life is just good.
words for my brother: i miss you
problem
the longest distance of a binary tree(problem from beauty of programming)
eg: the following binary tree
the longest distance is 8
my solution
experimentrecursion solution
int _Longest_distance(node * T,int & deep) { if(T == NULL) { deep = 0; return 0; } else { int left = 0; int right = 0; int Ldeep = 0; int Rdeep = 0; left = _Longest_distance(T->left,Ldeep); right = _Longest_distance(T->right,Rdeep); deep = (Ldeep > Rdeep? Ldeep:Rdeep) + 1; int result = left > right?left :right; result = result> (Ldeep + Rdeep)? result:(Ldeep+Rdeep); return result ; } }
program run out: 7
my code
test.cpp
#include<iostream> using namespace std; class node { public: int data ; node * left ; node * right ; node() { data = http://www.mamicode.com/0 ;>
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。