Java - depth of binary tree

topic link

Niuke.com online oj question - the depth of the binary tree

topic description

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

Data range: the number of nodes satisfies 0≤n≤100, and the value on the node satisfies 0≤val≤100

Advanced: space complexity O(1), time complexity O(n)

If the input use case is {1,2,3,4,5,#,6,#,#,7}, then as shown below:
insert image description here

Topic example

Example 1

Input:
{1,2,3,4,5,#,6,#,#,7}

return value:
4

Example 2

input:
{}

return value:
0

Problem-solving ideas one

Use breadth-first search to perform layer-order traversal of the binary tree, and depth++ will be used every time a layer is traversed

Breadth-first traversal requires the use of queues. First, add the root node to the queue, then determine the size of the queue each time, then pop up size elements, and add the left and right subtrees of these elements to the queue (if not null)

The above process of popping up size elements each time is the process of traversing one layer, so at this time, just set depth++

For example:
insert image description here
first add the root node to the queue, depth++
insert image description here

Now the length of the queue is 1, pop up 1 element, add its left subtree and right subtree to the queue, depth++
insert image description here
Now the length of the queue is 2, pop up 2 elements, add its left subtree and right subtree to the queue , depth++
insert image description here
Now the length of the queue is 3, pop up 3 elements, add its left subtree and right subtree to the queue, depth++
insert image description here

Now the length of the queue is 1, and 1 element pops up. At this time, the left subtree and right subtree of the element are both null, no more elements are added to the queue, the loop ends, depth = 4

Method 1 complete code

import java.util.*;
/**
public class TreeNode {
    int val = 0;
    TreeNode left = null;
    TreeNode right = null;

    public TreeNode(int val) {
        this.val = val;

    }

}
*/
public class Solution {
    
    
    public int TreeDepth(TreeNode root) {
    
    
       if(root == null){
    
    
            return 0;
        }

        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        int depth = 0;
        while(!queue.isEmpty()) {
    
    
            int size = queue.size();
            depth++;
            for (int i = 0; i < size; i++) {
    
    
                TreeNode cur = queue.poll();

                if (cur.left != null) {
    
    
                    queue.add(cur.left);
                }
                if(cur.right != null){
    
    
                    queue.add(cur.right);
                }
            }
        }
        return depth;
    }
}

Idea two

Depth-first search, determine the larger value of depth in the left and right subtrees respectively

Use recursion to determine the height of the left subtree and the height of the right subtree of the node. Every time you recurse to the next layer of nodes, you need to add depth + 1. If the length of depth is greater than max at this time, update the value of max to depth. The greater of the left and right subtree heights can be returned

Method 2 complete code

/**
public class TreeNode {
    int val = 0;
    TreeNode left = null;
    TreeNode right = null;

    public TreeNode(int val) {
        this.val = val;

    }
}
*/
public class Solution {
    
    
    public int TreeDepth(TreeNode root) {
    
    
       if(root == null){
    
    
           return 0;
       }
       
       int depth = 0;
       int[] max = new int[1];
       max[0] = 0;
       TreeDepthHelper(root, depth, max);
       return max[0];
    }

    private void TreeDepthHelper(TreeNode root, int depth, int[] max) {
    
    
        if(root == null){
    
    
            if(max[0] < depth){
    
    
                max[0] = depth;
            }
            return;
        }
        TreeDepthHelper(root.left, depth + 1, max);
        TreeDepthHelper(root.right, depth + 1, max); 
    }
}

Idea three

Similar to idea 2, it is easier to understand in form

We think that the null pointer at the bottom is the 0th layer, and go up and add one for each layer

Therefore, we only need to count the greater value of the height of the left subtree and the height of the right subtree, and then add 1 to get the height of the current node

Method 3 complete code

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

Guess you like

Origin blog.csdn.net/m0_60867520/article/details/130493274