Java如何求树的直径
发表于:2025-12-02 作者:千家信息网编辑
千家信息网最后更新 2025年12月02日,本篇内容主要讲解"Java如何求树的直径",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"Java如何求树的直径"吧!package com.lifeibig
千家信息网最后更新 2025年12月02日Java如何求树的直径
本篇内容主要讲解"Java如何求树的直径",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"Java如何求树的直径"吧!
package com.lifeibigdata.algorithms.blog;import java.util.ArrayList;import java.util.List;/** * Created by lifei on 16/6/22. */public class MaxLenInBinTree { /* a. 1 / \ 2 3 / \ / \ 4 5 6 7 max=4 pass "root" b. 1 / \ 2 3 / \ 4 5 / \ 6 7 / \ 8 9 max=6. do not pass "root" */ private int maxLen=0; public static void main(String[] args) { int[] a={1,2,3,4,5,6,7}; //层级遍历 //store in LevelOrder,Complete Binary Tree. 0==no child MaxLenInBinTree m=new MaxLenInBinTree(); Node aRoot=m.createTree(a); m.findMaxLen(aRoot); System.out.println(m.maxLen); int[] b={1,2,3,4,5,0,0,6,0,0,7,0,0,0,0,8,0,0,0,0,0,0,9}; Node bRoot=m.createTree(b); m.findMaxLen(bRoot); System.out.println(m.maxLen); } public void findMaxLen(Node node){ if(node==null) return ; if(node.getLeft()==null){ node.setMaxLeftLen(0); } if(node.getRight()==null){ node.setMaxRightLen(0); } if(node.getLeft()!=null){ findMaxLen(node.getLeft()); } if(node.getRight()!=null){ findMaxLen(node.getRight()); } if(node.getLeft()!=null){ int temp=0; Node left=node.getLeft(); if(left.getMaxLeftLen()>left.getMaxRightLen()){ temp=left.getMaxLeftLen(); }else{ temp=left.getMaxRightLen(); } node.setMaxLeftLen(temp+1); } if(node.getRight()!=null){ int temp=0; Node right=node.getRight(); if(right.getMaxLeftLen()>right.getMaxRightLen()){ temp=right.getMaxLeftLen(); }else{ temp=right.getMaxRightLen(); } node.setMaxRightLen(temp+1); } if(maxLen nodeList=new ArrayList(); for(int each:data){ Node n=new Node(each); nodeList.add(n); } int lastRootIndex=data.length/2-1; for(int i=0;i<=lastRootIndex;i++){ Node root=nodeList.get(i); Node left=nodeList.get(i*2+1); if(left.getData()!=0){ root.setLeft(left); }else{ root.setLeft(null); } if(i*2+2到此,相信大家对"Java如何求树的直径"有了更深的了解,不妨来实际操作一番吧!这里是网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
直径
内容
学习
实用
更深
兴趣
实用性
实际
层级
操作简单
方法
更多
朋友
网站
频道
a.
b.
查询
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
绩溪常规软件开发服务有几种
贵阳东塔网络安全学院老品牌
各专业通用学术型数据库
山东蓝翔技校计算机网络技术
大专网络技术实习工资
八百元八核的服务器
哈工程网络安全学院
自学软件开发有公司要吗
旅游软件开发外包
美股网络安全系统
爬架网络安全网
每个网站都有一个自己的服务器吗
数据库求候选吗
软件开发中国有限公司
服务器市场报告 x86
合肥二手服务器回收在线咨询
武汉鹏洋网络技术有限公司
hp塔式服务器微型
网络安全法由什么机构制定
上海游卡网络技术有限公司
网络安全单词表
大家庭下载软件开发
江苏工控网络安全
新建数据库能够一步执行
兴化威力网络技术价目表
石家庄网络安全启动仪式
至尊网络安全
阿里云访问本地数据库
职高计算机与网络技术
戴尔服务器查看管理卡密码