Java language describes the depth and width of binary tree
Explanation:
Depth of binary tree: the nodes (including root and leaf nodes) passing from root node to leaf node form a path of the tree, and the length of the longest path is the depth of the tree. Width of binary tree: there are a certain number of nodes in each layer of the binary tree, and the number of nodes in the layer with the largest number of nodes is called the width of binary tree.
Idea: recursive implementation.
1. Each node can be regarded as a root node 2 The depth of the root node (any node) is equal to the maximum depth of its left or right subtree + 1 3. Traverse from the root node. If you traverse to the leaf node, the depth is 0
2、 Width of binary tree
Idea: add a counter during sequence traversal to record the number of nodes in each layer
1. When each layer goes out of the queue, record the number of nodes in the next layer, which is actually the size () 2 of the queue At the end of each layer traversal, compare the maximum width with the number of nodes in the current layer and record the maximum value
summary
The above is all about the depth and width of binary tree described by java language in this paper. I hope it will be helpful to you. If there are deficiencies, please leave a message to point out. Thank you for your support!