版权声明:欢迎查看本博客。希望对你有有所帮助 https://blog.csdn.net/cqs_2012/article/details/24880735

the longest distance of a binary tree

个人信息:就读于燕大本科软件project专业 眼下大三;

本人博客:google搜索“cqs_2012”就可以;

个人爱好:酷爱数据结构和算法,希望将来从事算法工作为人民作出自己的贡献;

博客内容:the longest distance of a binary tree;

博客时间:2014-4-26;

编程语言: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 

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvY3FzX2V4cGVyaW1lbnQ=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />

problem

the longest distance of a binary tree(problem from beauty of programming)

eg: the following binary tree

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvY3FzX2V4cGVyaW1lbnQ=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />

the longest distance is 8 

my solution 

recursion 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 ;
}
}

experiment

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvY3FzX2V4cGVyaW1lbnQ=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />

program run out: 7

my code

test.cpp

#include<iostream>
using namespace std; class node
{
public:
int data ;
node * left ;
node * right ;
node()
{
data = 0 ;
left = right = NULL ;
}
}; void _MakeTree(node * &T,int *data,int length);
void _Insert(node * & T,int data); void _MakeTree(node * &T,int *data,int length)
{
for(int i=0;i<length;i++)
{
_Insert(T,data[i]);
}
} void _Insert(node * & T,int data)
{
if(T == NULL)
{
T = new node();
T -> data = data;
}
else
{
node * p = T;
while(p != NULL)
{
if(data == p->data )
break;
else if(data < p->data )
{
if(p->left != NULL)
{
p = p ->left;
}
else{
p->left = new node();
(p->left) ->data = data;
break;
}
}
else{
if(p->right != NULL)
p = p->right;
else{
p->right = new node() ;
(p->right) ->data = data ;
break ;
}
}
}
}
} 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 ;
}
} int main()
{
int data[] = {8,6,3,7,2,1,13,12,15,17}; node * T = NULL;
_MakeTree(T,data,10);
int deep =0;
cout<<_Longest_distance(T,deep)<<endl;
system("pause");
return 0;
}

the longest distance of a binary tree的更多相关文章

  1. 【LeetCode】863. All Nodes Distance K in Binary Tree 解题报告(Python)

    [LeetCode]863. All Nodes Distance K in Binary Tree 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http ...

  2. [LeetCode] All Nodes Distance K in Binary Tree 二叉树距离为K的所有结点

    We are given a binary tree (with root node root), a target node, and an integer value K. Return a li ...

  3. [Swift]LeetCode863. 二叉树中所有距离为 K 的结点 | All Nodes Distance K in Binary Tree

    We are given a binary tree (with root node root), a targetnode, and an integer value K. Return a lis ...

  4. 863. All Nodes Distance K in Binary Tree 到制定节点距离为k的节点

    [抄题]: We are given a binary tree (with root node root), a target node, and an integer value K. Retur ...

  5. LeetCode – All Nodes Distance K in Binary Tree

    We are given a binary tree (with root node root), a target node, and an integer value K. Return a li ...

  6. leetcode 863. All Nodes Distance K in Binary Tree

    We are given a binary tree (with root node root), a target node, and an integer value K. Return a li ...

  7. [LC] 863. All Nodes Distance K in Binary Tree

    We are given a binary tree (with root node root), a target node, and an integer value K. Return a li ...

  8. 863. All Nodes Distance K in Binary Tree

    /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode ...

  9. 距离为K的节点 All Nodes Distance K in Binary Tree

    2018-07-26 17:38:37 问题描述: 问题求解: 解法一. 第一种解法是使用Graph + BFS.换言之,就是将二叉树转化为无向图,然后在无向图中使用BFS进行层次遍历即可. 这种解法 ...

随机推荐

  1. 在方法中new关键字的用处

    如果在类A中有M1这个方法需方法 public virtual ovid m1() { console.writeline(“我的世界”); } 那么你在类B中继承的时候可以重写这个方法,也可以不重写 ...

  2. thinkphp htmlspecialchars_decode

    一  百度编辑器 与 htmlspecialchars_decode *Thinkphp百度编辑器 存的时候为了安全把进行了字符转换,数据库: <p>&nbsp;测试测试</ ...

  3. python学习之老男孩python全栈第九期_day021知识点总结——包、异常处理

    一. 包 # 把解决一类问题的模块放在同一个文件夹里 -- 包 # 创建目录代码# import os# os.makedirs('glance/api')# os.makedirs('glance/ ...

  4. 【HTML&CSS】基本的入门

    在公司培训一段时间不久就去流浪了一段时间,现在回来重新捧起心爱的编程,特别亲切. 自学HTML&CSS,别人说了很多,这那这那的,无论简单还是困难,不亲自去俯下身子学习,怎么都学不会HTML和 ...

  5. window.addEventListener绑定事件记得删除

    在做postMessage通信时,window.addEventListener绑定的事件记得要remove掉 就和setTime一样,不然占用内存资源

  6. redux-devtools的使用

    1.浏览器里安装redux-devtools 2.在项目的入口文件里  找到 createStore函数调用的地方,给它加第二个参数 window.__REDUX_DEVTOOLS_EXTENSION ...

  7. Portal for ArcGIS 10.2.2更改域名和导入自定义证书

    1.产品版本 Portal for ArcGIS10.2.2(同样适用于ArcGIS10.3) 2.修改说明 )修改Portal中的域名:(2)修改Portal中的证书. 3.修改步骤 3.1.在ho ...

  8. 移动端App开发 - 02 - iPhone/iPad/Android UI尺寸规范

    移动端app开发 - iPhone/iPad/Android UI尺寸规范 本笔记抛去无用的前期分析什么的,全是干货,简洁干练 本笔记不单独针对 ios 或者 Android,两种都介绍,当然我们实际 ...

  9. 5 使用ip代理池爬取糗事百科

    从09年读本科开始学计算机以来,一直在迷茫中度过,很想学些东西,做些事情,却往往陷进一些技术细节而蹉跎时光.直到最近几个月,才明白程序员的意义并不是要搞清楚所有代码细节,而是要有更宏高的方向,要有更专 ...

  10. Java基础之PDF文件的合并

    1.首先下载一个jar包:pdfbox-app-1.7.1.jar 2.代码如下: package com; import java.io.File; import java.io.IOExcepti ...