Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Depth example Analysis of Java binary Tree

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the relevant knowledge of "in-depth example Analysis of Java binary Tree". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Title

Enter a binary tree to find the depth of the tree. The nodes (including root and leaf nodes) passing through from the root node to the leaf node form a path of the tree, and the length of the longest path is the depth of the tree.

Analysis.

The main way to find the depth of the tree is to look at the longest path. For example, the depth of the image below is 5, and the longest path is 34, 99, 35, 64, 77.

What should I do? Recursion is used here. If the current node does not have a left or right node, it will return to the current node. If there is a left or right node, it will return to the left node. Compare the depth of the left node with that of the right node. Whoever has the greater depth will return to that node. In this way, the maximum depth of the tree can be obtained.

Solution public int TreeDepth (TreeNode root) {

If (root==null) {

Return 0

}

Int left=TreeDepth (root.left)

Int right=TreeDepth (root.right)

If (left > right) {

Return left+1

}

Return right+1

}

The main note above is why left+1 and right+1; should be added one, because our recursive exit is that the current node is null, returns 0, and returns 1. 0 for 1 node.

test

Test main method

Public static void main (String [] args) {

TreeNode root = new TreeNode (34)

Root.left=new TreeNode (23)

Root.right=new TreeNode (99)

Root.left.left=new TreeNode (1)

Root.left.right=new TreeNode (27)

Root.right.left=new TreeNode (35)

Root.right.left.right=new TreeNode (64)

Root.right.left.right.right=new TreeNode (77)

TreeOperation.show (root)

Solution solution= new Solution ()

System.out.println (solution.TreeDepth (root))

}

This is the end of the content of "Deep example Analysis of Java binary Tree". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report