Maximum/minimum depth of binary tree

1. What is the depth of a binary tree?

The depth of a binary tree node: refers to the number of edges or nodes of the longest simple path from the root node to the node. If it is the number of edges, the depth starts from 0; if it is the number of nodes, the depth starts from 1. Generally, the root node must be traversed first, so the preorder traversal method is often used to find the depth.

The height of a binary tree node: refers to the number of longest simple path edges or nodes from the node to the leaf node. Generally, it is necessary to traverse to the leaf nodes, so the post-order traversal method is often used to find the depth.

The height of the root node is the maximum depth of the binary tree, so you can also use the method of preorder traversal to find the maximum depth of the binary tree (that is, the height of the root node).


Second, find the maximum depth of the binary tree

recursion

//后序遍历
int getDepth(Treenode* node) {
        if (node == NULL) return 0;
        int leftdepth = getDepth(node->left);       // 左
        int rightdepth = getDepth(node->right);     // 右
        int depth = 1 + max(leftdepth, rightdepth); // 中
        return depth;
    }

int maxDepth(Treenode* root) {
        return getDepth(root);
    }
//前序遍历
int result;
void getDepth(treenode* node, int depth) {
     result = max(depth, result); // 中

     if (node->left == NULL && node->right == NULL) return ;

     if (node->left) { // 左
         getDepth(node->left, depth + 1);
     }

     if (node->right) { // 右
         getDepth(node->right, depth + 1);
     }
     return ;
    }

int maxDepth(treenode* root) {
    result = 0;
    if (root == 0) return result;
    getDepth(root, 1);
    return result;
    }

iteration

//层序遍历
int maxDepth(Treenode* root) {
        if (root == NULL) return 0;
        int depth = 0;
        queue<Treenode*> que;
        que.push(root);
        while(!que.empty()) {
            int size = que.size();
            depth++; 
            for (int i = 0; i < size; i++) {
                treenode* node = que.front();
                que.pop();
                if (node->left) que.push(node->left);
                if (node->right) que.push(node->right);
            }
        }
        return depth;
    }

3. The minimum depth of the binary tree

recursion

//后序遍历
int getDepth(TreeNode* node) {
    if (node == NULL) return 0;
    int leftDepth = getDepth(node->left);           // 左
    int rightDepth = getDepth(node->right);         // 右
                                                    // 中

    //只有当这个节点左右孩子都为空时,该节点才是叶子节点
    // 当一个左子树为空,右不为空,这时并不是最低点
    if (node->left == NULL && node->right != NULL) { 
        return 1 + rightDepth;
    }   
    // 当一个右子树为空,左不为空,这时并不是最低点
    if (node->left != NULL && node->right == NULL) { 
        return 1 + leftDepth;
    }
    return 1 + min(leftDepth, rightDepth);
}

int minDepth(TreeNode* root) {
    int result = getDepth(root);
    return result;
}
//前序遍历
int result;
void getDepth(TreeNode* node, int depth) {
   if (node->left == NULL && node->right == NULL) {
        result = min(depth, result);  
        return;
    }
    // 中 只不过中没有处理的逻辑
    if (node->left) { // 左
        getDepth(node->left, depth + 1);
    }
    if (node->right) { // 右
        getDepth(node->right, depth + 1);
    }
    return ;
}

int minDepth(TreeNode* root) {
    if (root == NULL) return 0;
    result = INT_MAX;
    getDepth(root, 1);
    return result;
}

iteration

//迭代法
int minDepth(TreeNode* root) {
    if (root == NULL) return 0;
    int depth = 0;
    queue<TreeNode*> que;
    que.push(root);
    while(!que.empty()) {
        int size = que.size();
        depth++; // 记录最小深度
        for (int i = 0; i < size; i++) {
            TreeNode* node = que.front();
            que.pop();
            if (node->left) que.push(node->left);
            if (node->right) que.push(node->right);
            if (!node->left && !node->right) { // 当左右孩子都为空的时候,说明是最低点的一层了,退出
                return depth;
            }
        }
    }
    return depth;
}

Guess you like

Origin blog.csdn.net/qq_52686989/article/details/130154938