In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains "how to find the diameter of a tree by Java". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how to find the diameter of a tree by 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 /\ 45 /\ 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}; / / hierarchical traversal / / 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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } 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)
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.