Tree Diameter
来源:互联网 发布:生成对抗网络 论文 编辑:程序博客网 时间:2024/06/01 09:19
The function below returns the diameter of a tree. A tree's diameter isdefined after the function. Write a recurrence for this function andsolve it yielding the running time using big-Oh in terms of the numberof nodes in a tree.
int diameter(Tree * t) // post: return diameter of t{ if (t == 0) return 0; int lheight = height(tree->left); int rheight = height(tree->right); int ldiameter = diameter(tree->left); int rdiameter = diameter(tree->right); return max(lheight + rheight + 1, max(ldiameter,rdiameter));}The following function returns the height of a tree (the numberof nodes on the longest root-to-leaf path).
int height(Tree * t) // postcondition: returns height of tree with root t{ if (t == 0) return 0; else return (1 + max(height(t->left), height(t->right)); }where the function max returns the largest of its two integerparameters. Thediameter of a tree (sometimes called the width) is the numberof nodes on the longest path between two leaves in the tree. Thediagram below shows two trees each with diameter nine, the leaves thatform the ends of a longest path are shaded (note that there is more thanone path in each tree of length nine, but no path longer than nine nodes).
The diameter of a tree T is the largest of thefollowing quantities:
- the diameter of T's left subtree
- the diameter of T's right subtree
- the longest path between leaves that goes through the root of T(this can be computed from the heights of the subtrees ofT)
0 0
- Tree Diameter
- Diameter of Binary Tree
- Diameter of Binary Tree
- Diameter of Binary Tree
- Diameter of Binary Tree
- Maximal Tree Diameter
- Diameter of Binary Tree
- 543. Diameter of Binary Tree
- 543. Diameter of Binary Tree
- 543. Diameter of Binary Tree
- 543. Diameter of Binary Tree
- 543. Diameter of Binary Tree
- LeetCode543. Diameter of Binary Tree
- 543. Diameter of Binary Tree
- 543. Diameter of Binary Tree
- 543. Diameter of Binary Tree
- 543. Diameter of Binary Tree
- 543. Diameter of Binary Tree
- 托管线程处理的最佳做法
- hadoop备战:yarn框架的简介(mapreduce2)
- 跨模块传参数的教训
- mvn 创建java项目 web项目
- OCP 1Z0 051 169
- Tree Diameter
- 相对路径和绝对路径的区别
- VC++GDI+绘制字体
- 金妍儿身价超过顶级艺人 笑言索契卫冕压力小
- Java读取txt格式文件打印到控制台
- 黑马程序员 多线程死锁的问题.
- oracle不能使用索引
- SVN与eclipse整合与使用、SVN与Apache整合
- javascript语法详解(六)----语句