C# Code Caprice Algorithm Training Camp day23|Prune the binary search tree, convert the ordered array to a binary search tree, convert the binary search tree to an accumulation tree

LeetCode669 Pruning Binary Search Tree

topic:

Give you the root node root of the binary search tree, and give you the minimum boundary low and the maximum boundary high. Prune the binary search tree so that the values ​​of all nodes are in [low, high]. Pruning the tree should not change the relative structure of elements that remain in the tree (i.e., if not removed, the original parent-child relationships should be retained). It can be proven that there is a unique answer.
So the result should return the new root node of the pruned binary search tree. Note that the root node may change based on the given bounds.

Example 1:
Insert image description here

Input: root = [1,0,2], low = 1, high = 2
Output: [1,null,2]

Example 2:
Insert image description here

Input: root = [3,0,4,null,2,null,null,1], low = 1, high = 3
Output: [3,2,null,1]

Code:
//DFS
public class Solution {
    
    
    public TreeNode TrimBST(TreeNode root, int low, int high) {
    
    
        if (root == null) return root;
        if (root.val < low)
        {
    
    
            return TrimBST(root.right, low, high);
        }
        if (root.val > high)
        {
    
    
            return TrimBST(root.left, low, high);
        }
        root.left = TrimBST(root.left, low, high);
        root.right = TrimBST(root.right, low, high);
        return root;
    }
}

LeetCode108 Convert ordered array to binary search tree

topic:

You are given an integer array nums, in which the elements have been sorted in ascending order. Please convert it into a height-balanced binary search tree.
A height-balanced binary tree is a binary tree that satisfies "the absolute value of the height difference between the left and right subtrees of each node does not exceed 1".

Example 1:
Insert image description here

Input: nums = [-10,-3,0,5,9]
Output: [0,-3,9,-10,null,5]
Explanation: [0,-10,5,null,-3,null ,9] will also be considered the correct answer:Insert image description here

Example 2:
Insert image description here

Input: nums = [1,3]
Output: [3,1]
Explanation: [1,null,3] and [3,1] are both highly balanced binary search trees.

Code:
//DFS
public class Solution {
    
    
    public TreeNode SortedArrayToBST(int[] nums) {
    
    
        TreeNode root = DFS(nums, 0, nums.Length - 1);
        return root;
    }

    public TreeNode DFS(int[] nums, int left, int right)
    {
    
    
        if (left > right) return null;
        int mid = left + (right - left) / 2;
        TreeNode root = new TreeNode(nums[mid]);
        root.left = DFS(nums, left, mid - 1);
        root.right = DFS(nums, mid + 1, right);
        return root;
    }
}

LeetCode538 Convert binary search tree to cumulative tree

topic:

Given the root node of a binary search tree, the node values ​​of this tree are different. Please convert it to a Greater Sum Tree so that the new value of each node is equal to the node in the original tree that is greater than or equal to it. The sum of the values ​​of val.
As a reminder, a binary search tree satisfies the following constraint:
The left subtree of a node contains only nodes with keys less than the node's key.
The right subtree of a node contains only nodes with keys greater than the node's key.
The left and right subtrees must also be binary search trees.

Example 1:
Insert image description here

输入:[4,1,6,0,2,5,7,null,null,null,3,null,null,null,8]
输出:[30,36,21,36,35,26,15,null,null,null,33,null,null,null,8]

Example 2:
Input: root = [0,null,1]
Output: [1,null,1]

Example 3:
Input: root = [1,0,2]
Output: [3,3,2]

Example 4:
Input: root = [3,2,4,1]
Output: [7,9,4,10]

Code:
//DFS
int sum = 0;//注意要定义在函数体外面,因为需要在每层递归时记录sum的值,定义在函数内每次递归都从0开始,不合题意
public class Solution {
    
    
    public TreeNode ConvertBST(TreeNode root)
    {
    
    
        if (root != null)
        {
    
    
            ConvertBST(root.right);
            sum += root.val;
            root.val = sum;
            ConvertBST(root.left);
        }
        return root;
    }
}

Guess you like

Origin blog.csdn.net/weixin_44740741/article/details/129978515