首页 > 其他分享 >tree_diameter

tree_diameter

时间:2023-05-31 20:34:23浏览次数:39  
标签:diameter right return int max tree height

 

 

 

 

public static int height(BinTree T) {
if (T == null) {
return -1;
} else {
return Math.max(height(T.left), height(T.right)) + 1;
}
}
/** Return the diameter of T. */
public static int diameter(BinTree T) {
if (T == null ) {
return 0;
} else {
int childDiam = Math.max(diameter(T.left), diameter(T.right));
int nodeDiam = 2 + height(T.left) + height(T.right);
return Math.max(childDiam, nodeDiam);
}

 

标签:diameter,right,return,int,max,tree,height
From: https://www.cnblogs.com/xuenima/p/17447250.html

相关文章

  • 如何使用TreeView展示树状数据
    如何使用TreeView展示树状数据TreeView是一个可用于显示树形数据结构的UI组件。它提供了一个可折叠、可展开的树状视图。TreeView是一个树状结构,其根节点的类型是TreeItem。每个TreeItem又可以包含若干TreeItem。由此可组成一颗树形结构。效果展示示例代码importj......
  • SBT模版(Size Balanced Tree)
    关于SBT的介绍及学习,请戳这里。 SBT模版:/*************************************************数据结构:SBT(SizeBalancedTree),又称傻逼树;数据域:值域key,左孩子left,右孩子right,保持平衡的size;性质:每棵子树的大小不小于其兄弟的子树大小;插入:插入算法先简单插入节......
  • mongodb压缩——snappy、zlib块压缩,btree索引前缀压缩
    MongoDB3.0WiredTigerCompressionandPerformanceOneofthemostexcitingdevelopmentsoverthelifetimeofMongoDBmustbetheinclusionoftheWiredTigerstorageengineinMongoDB3.0.Itsverydesignandcorearchitecturearelegionsaheadofthecurr......
  • python berkeley DB操作——打开btree索引文件中的database
    打开BDB中某个索引中的数据库代码: frombsddb3importdbimportbsddb3asbsddbprintdb.DB_VERSION_STRINGmydb=db.DB()mydb.open('your_btree_db_filename','databsename',dbtype=db.DB_BTREE)rec=cur.first()whilerec:#printkeyvaluepri......
  • SimpleAdmin手摸手教学之:基于Ant Design Tree组件实现树形结构数据的异步加载
    一、说明当有一个树形结构的数据有非常多个节点的时候,一次性加载所有节点会显得过于臃肿,可能会对性能造成影响,正好AntDesign的树(Tree)组件支持异步加载,于是我就想把异步加载封装为一个组件,可以减少接口数据返回,点击展开节点,动态加载数据。非常好用!二、前端实现需要接收一些......
  • ERESOLVE unable to resolve dependency tree
    错误描述:报错原因(据查):依赖项中存在无法解决的冲突解决方法:使用如下命令npmi--legacy-peer-deps运行结果:......
  • leetcode 637. Average of Levels in Binary Tree
    Givenanon-emptybinarytree,returntheaveragevalueofthenodesoneachlevelintheformofanarray.Example1:Input:3/\920/\157Output:[3,14.5,11]Explanation:Theaveragevalueofnodesonlevel0is3,onlevel......
  • leetcode 669. Trim a Binary Search Tree
    GivenabinarysearchtreeandthelowestandhighestboundariesasLandR,trimthetreesothatallitselementsliesin[L,R](R>=L).Youmightneedtochangetherootofthetree,sotheresultshouldreturnthenewrootofthetrimmedbinarys......
  • leetcode 617. Merge Two Binary Trees
    Giventwobinarytreesandimaginethatwhenyouputoneofthemtocovertheother,somenodesofthetwotreesareoverlappedwhiletheothersarenot.Youneedtomergethemintoanewbinarytree.Themergeruleisthatiftwonodesoverlap,thensumn......
  • leetcode 257. Binary Tree Paths
    Givenabinarytree,returnallroot-to-leafpaths.Forexample,giventhefollowingbinarytree: 1/\23\5Allroot-to-leafpathsare:["1->2->5","1->3"]#Definitionforabinarytreenode.#classTreeNode(obje......