【LeetCode】Jianzhi Offer(28)

Table of contents

Topic: Sword Pointing to Offer 54. The kth Largest Node of a Binary Search Tree - Leetcode

The interface of the topic:

Problem-solving ideas:

code:

It's over! ! !

Title: Sword Pointing to Offer 55 - I. Depth of Binary Tree - Leetcode

The interface of the topic:

Problem-solving ideas:

code:

It's over! ! !

Title: Sword Pointer Offer 55 - II. Balanced Binary Tree - Leetcode

The interface of the topic:

Problem-solving ideas:

code:

It's over! ! !

Write at the end:


Topic: Sword Pointing to Offer 54. The kth Largest Node of a Binary Search Tree - Leetcode

The interface of the topic:

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    int kthLargest(TreeNode* root, int k) {

    }
};

Problem-solving ideas:

Because the characteristic of a balanced binary tree is that the in-order traversal is an ascending array,

The title requires finding the kth largest value,

It is not difficult to imagine that we only need to traverse the balanced binary tree in reverse order,

Let k-- every time, as long as k==0, it means that it is found:

code:

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    int kthLargest(TreeNode* root, int k) {
        //走中序遍历
        dfs(root, k);
        return ans;
    }
private:
    //记录k节点的值
    int ans = 0;

    //走一个倒序的中序遍历,让k值每走一个节点就--
    void dfs(TreeNode* root, int& k) {
        if(root == nullptr) return;
        dfs(root->right, k);

        //找到题目要求节点,记录ans值
        if(--k == 0) {
            ans = root->val;
            return;
        }
        dfs(root->left, k);
    }
};

It's over! ! !

Title: Sword Pointing to Offer 55 - I. Depth of Binary Tree - Leetcode

The interface of the topic:

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    int maxDepth(TreeNode* root) {

    }
};

Problem-solving ideas:

My idea is to calculate the depth of the left and right subtrees of each subtree,

Then compare the depth of each left and right subtree, save the maximum value,

The specific analysis is shown in the figure:

 By continuously calculating the maximum depth of each subtree,

Finally, the maximum depth of the entire tree is obtained

Here is the code:

code:

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    int maxDepth(TreeNode* root) {
        if(root == nullptr) return 0;
        int left = maxDepth(root->left); //求出左边高度
        int right = maxDepth(root->right); //求出右边高度
        return max(left, right) + 1; //每层 + 1
    }
};

It's over! ! !

Title: Sword Pointer Offer 55 - II. Balanced Binary Tree - Leetcode

The interface of the topic:

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    bool isBalanced(TreeNode* root) {

    }
};

Problem-solving ideas:

The specific idea is,

We calculate the maximum depth difference between the left and right subtrees,

If the maximum depth difference between the left and right subtrees >= 2, it proves that it is not a balanced binary tree.

If < 2, it proves that the subtree itself is a balanced binary tree, then calculate its own maximum depth normally,

The left and right subtrees up to the root node still do not return -1 and the depth meets the requirements, which proves that it is a balanced binary tree.

If -1 is returned, it proves that it is not a balanced binary tree.

The idea of ​​​​calculating the maximum depth here also follows the idea of ​​​​the previous question.

Here is the code:

code:

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    bool isBalanced(TreeNode* root) {

        //判断如果返回-1就证明不是平衡二叉树
        return recur(root) != -1;
    }
private:
    int recur(TreeNode* root) {
        if(root == nullptr) return 0;

        //计算左右子树最大深度,如果出现-1证明不是平衡二叉树,返回-1就行
        int left = recur(root->left);
        if(left == -1) return -1;
        int right = recur(root->right);
        if(right == -1) return -1;

        //核心代码:如果左右子树最大深度正常,就正常计算左右深度的最大值
        //如果左右子树的最大深度差大于2,就证明这不是一个平衡二叉,返回-1
        return abs(left - right) < 2 ? max(left, right) + 1 : -1; 
    }
};

It's over! ! !

Write at the end:

The above is the content of this article, thank you for reading.

If you like this article, please like and comment, and write down your opinions.

If you want to learn programming with me, you might as well follow me, we will learn and grow together.

I will output more high-quality content in the future, welcome to watch

Guess you like

Origin blog.csdn.net/Locky136/article/details/130077742