【LeetCode】HOT 100(15)

Introduction to the question list:

Selected 100 most popular questions on LeetCode, suitable for beginners who are new to algorithms and data structures and those who want to improve efficiently in a short period of time, master these 100 questions, and you already have the ability to learn in code The basic ability to pass through the world.

Table of contents

Introduction to the question list:

Topic: 98. Verifying a Binary Search Tree - Leetcode

The interface of the topic:

Problem-solving ideas:

code:

It's over! ! ! !

Topic: 114. Binary tree expanded into a linked list - Leetcode

The interface of the topic:

Problem-solving ideas:

code:

It's over! ! ! !

Write at the end:


Title: 98. Verifying 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() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    bool isValidBST(TreeNode* root) {

    }
};

Problem-solving ideas:

For this question, I used a binary search tree for judgment by preorder traversal.

Personally, I think preorder traversal is relatively simple,

The main ideas are as follows:

The preorder traverses the entire tree and records the value of the previous node,

When walking the left subtree, the value of the current node is smaller than the value of the previous node,

When walking the right subtree, the value of the current node is greater than the value of the previous node,

We just pass the value of the previous node directly through the function parameter,

In addition, when I submitted this question at the beginning, it was violently int, so I had to change it to long

code show as below:

code:

class Solution {
public:
    bool isValidBST(TreeNode* root, long left = LONG_MIN, long right = LONG_MAX) {
        if(root == nullptr) return true;
        long cur = root->val;
        return left < cur 
            && right > cur 
            && isValidBST(root->left, left, cur) //这里传的cur其实就是将上一个节点的值传下来
            && isValidBST(root->right, cur, right);
    }
};

It's over! ! ! !

Topic: 114. Binary tree expanded into a linked list - Leetcode

The interface of the topic:

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

    }
};

Problem-solving ideas:

First of all, I can't do the advanced conditional O(1) algorithm,

Then there is the idea of ​​this question, I really don’t know how they came up with it,

My idea at the beginning was to traverse the previous order, get the value and store it in the array.

Then use this array to make a linked list, but it needs to be converted in place, and it doesn't feel feasible, so I use other methods.

The specific ideas are as follows:

Graft the left subtree to the right subtree, let the original right subtree be grafted to the rightmost node of the left subtree,

Pre-order traversal can be simulated only until there is no left subtree (all empty according to the requirements of the title),

If you have any questions, you can draw a picture and see for yourself, I won’t demonstrate it here

code show as below:

code:

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    void flatten(TreeNode* root) {
        while(root != nullptr) {
            if(root->left == nullptr) { //左子树为空
                root = root->right;
            }
            else { //存在左节点
                TreeNode* prev = root->left;
                while(prev->right) prev = prev->right; //找到左子树的最右节点 
                prev->right = root->right; //将原来的右子树移植到左子树的最右节点 
                root->right = root->left; //将左子树插入进右子树
                root->left = nullptr; //题目的要求,将左节点置空
                root = root->right; //继续找下一个左子树不为空的节点
            }
        }
    }
};

It's over! ! ! !

Write at the end:

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

If you feel that you have gained something, you can give the blogger a like .

If there are omissions or mistakes in the content of the article, please private message the blogger or point it out in the comment area~

Guess you like

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