[LeetCode-150 classic interview questions-day25]

Table of contents

530. Minimum absolute difference of binary search tree

 230.The Kth smallest element in the binary search tree

 98. Verify binary search tree


 

530. Minimum absolute difference of binary search tree

Question meaning:

Given the root node of a binary search tree  root , return  the minimum difference between the values ​​of any two different nodes in the tree  .

Difference is a positive number whose value is equal to the absolute value of the difference between two values.

  • The range of the number of nodes in the tree is [2, 100]
  • 0 <= Node.val <= 105

[Input example] root = [4,2,6,1,3]

【Output sample】1

Problem-solving ideas:

The characteristics of a binary search tree are that all values ​​in the left subtree are smaller than the root node, and all values ​​in the right subtree are larger than the root node;

To find the minimum difference, the choice must be the minimum difference between two adjacent elements.

Calculate the difference by traversing the tree, save the results, and compare while traversing.

The traversal method uses in-order traversal because the results of in-order traversal are ordered.

class Solution {
    int ans,pre;//Pre保存前驱节点的值
    public int getMinimumDifference(TreeNode root) {
        ans = Integer.MAX_VALUE;
        pre = -1;//提示中说明节点值的范围是[0,105],所以初始化为-1
        midOrder(root);
        return ans;      
    }

    void midOrder(TreeNode root){
        if(root == null){
            return;
        }
        midOrder(root.left);
        if(pre == -1){
            pre = root.val;
        }else{
            ans = Math.min(ans, root.val - pre);
            pre = root.val;
        }
        midOrder(root.right);
    }
}

Time: Beat 100.00%

Memory: Beaten by 29.78%

 230.The Kth smallest element in the binary search tree

Question meaning:

Given the root node of a binary search tree  root and an integer  k , please design an algorithm to find the  k smallest element (counting from 1).

hint:

  • The number of nodes in the tree is  n .
  • 1 <= k <= n <= 104
  • 0 <= Node.val <= 104

[Input example] root = [3,1,4,null,2], k = 1

【Output sample】1

Problem-solving ideas:

Just find the kth number through in-order traversal.

In order to avoid traversing all nodes, recursive search is not used, but iterative search is used, with the help of stack

class Solution {
    public int kthSmallest(TreeNode root, int k) {
        Deque<TreeNode> stack = new ArrayDeque<TreeNode>();
        while(root != null || !stack.isEmpty()){
            while(root != null){
                stack.push(root);//先把根存进去,然后先找左子树
                root = root.left;//左 根 右,持续进栈根节点,直到找到最左的节点
            }
            //循环结束条件是root为空,证明上一个root没有左节点,出栈
            root = stack.pop();
            --k;//遍历到一个数减一次
            if(k == 0){
                break;
            }
            root = root.right;//往节点右子树找了
        }
        return root.val;
    }
}

Time: Defeated 24.31%

Memory: Beaten by 75.46%

 98. Verify binary search tree

Question meaning:

Given the root node of a binary tree  root , determine whether it is a valid binary search tree.

A valid  binary search tree is defined as follows:

  • The left subtree of a node only contains numbers  less than  the current node.
  • The right subtree of a node only contains  numbers greater than  the current node.
  • All left and right subtrees must themselves be binary search trees.

hint:

  • The number of nodes in the tree [1, 104] is within
  • -231 <= Node.val <= 231 - 1

[Input example] root = [2,1,3]

[Output sample] true

Problem-solving ideas:

Like the previous question, use the stack to perform in-order traversal, and use pre to save the value of the previous node. If root.val <= pre, it means it is not a binary search tree.

class Solution {
    public boolean isValidBST(TreeNode root) {
        double pre = -Double.MAX_VALUE;;
        Deque<TreeNode> stack = new ArrayDeque<TreeNode>();
        while(root != null || !stack.isEmpty()){
            while(root != null){
                stack.push(root);
                root = root.left;
            }
            root = stack.pop();
            if(root.val <= pre){//=也不符合二叉搜索树的要求
                return false;//不满足
            }
            pre = root.val;
            root = root.right;
        }
        return true;
    }
}

Time: Defeated 18.62%

Memory: Beaten by 49.12%

Guess you like

Origin blog.csdn.net/qq_37998848/article/details/132940544