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

How to find out the maximum depth of python binary tree

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

In this issue, the editor will bring you about how to find out the maximum depth of the python binary tree. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

1. Brief introduction of the problem.

Given a binary tree, find out its maximum depth. The depth of the binary tree is the number of nodes on the longest path from the root node to the furthest leaf node. Note: a leaf node is a node that does not have child nodes.

2, example

Given binary tree [3pc9pr 20pr nullpr nullpr 15pc7]

3 / 9 20 / 15 7 returns its maximum depth 3.

3, the train of thought of solving the problem

It is solved according to the recursive way, and here it is solved in another way according to the characteristics of the queue, but this way of the queue is still time-consuming.

4, problem solving procedure

Import java.util.LinkedList;import java.util.Queue

Public class MaxDepthTest2 {public static void main (String [] args) {TreeNode T1 = new TreeNode (3); TreeNode T2 = new TreeNode (9); TreeNode T3 = new TreeNode (20); TreeNode T4 = new TreeNode (15); TreeNode T5 = new TreeNode (7); t1.left = T2; t1.right = T3; t3.left = T4; t3.right = T5 Int maxDepth = maxDepth3 (T1); System.out.println ("maxDepth =" + maxDepth)

}

Public static int maxDepth (TreeNode root) {if (root = = null) {return 0;} return Math.max (maxDepth (root.left), maxDepth (root.right)) + 1;}

Public static int maxDepth3 (TreeNode root) {if (root = = null) {return 0;} Queue queue = new LinkedList (); queue.add (root); int level = 0; while (! queue.isEmpty ()) {int size = queue.size (); level++; for (int I = 0; I

< size; i++) { TreeNode treeNode = queue.poll(); if (treeNode.left != null) { queue.add(treeNode.left); } if (treeNode.right != null) { queue.add(treeNode.right); } } } return level; }} 5,题解程序图片版

The maximum depth of the binary tree is still commonly used. Here I give two ways to solve it, one is to solve it recursively, using the contents of the system stack to solve it, and the other is to solve it according to the characteristics of the queue. Here according to your own preferences to solve it, but I want to say this picture is particularly good-looking

The above is the editor for you to share how to find out the maximum depth of the python binary tree, if you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

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