LeetCode interview question 04.09. Binary search tree sequence

Article directory

1. Title

  Traversing an array from left to right, a binary search tree can be gradually generated by continuously inserting elements into the tree.

  Given a binary search tree consisting of different nodes root, output an array of all possible ways to generate this tree.

  Click here to jump to the question .

Example 1:

Input: root = [2,1,3]
Output: [[2,1,3],[2,3,1]]
Explanation: Both arrays [2,1,3] and [2,3,1] are acceptable The following binary search tree is formed by traversing the element insertion tree from left to right

   2 
  / \ 
 1   3

Example 2:

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

hint:

  • The number of nodes in a binary search tree is in [0, 1000]the range
  • 1 <= 节点值 <= 10^6
  • The use case ensures that the number of arrays that meet the requirements does not exceed5000

2. C# problem solution

  Analyzing the problem, the first thing to put in can only be the root node, followed by its left and right children. The process is as follows:

  1. First, you can put the root node: [2]
  2. After 2 is placed, its left and right children can be placed: [1,4]
  3. When 1 is placed, the following can be placed: [5,4]; when 4 is placed, the following can be placed: [1,6,7]

  Therefore, it can be found that this is a step-by-step process, that is, the nodes placed in the array each time can only be the children of the node currently placed, and cannot skip levels. Once you skip a level, for example, put in: [2,1,6] in sequence, you will find that when 6 is put in, 6 will directly become the right child of 2, breaking the level of 4.

Insert image description here
  Use the next array to record the nodes that can be accessed later, and the lst array to record each answer. The recursive backtracking solution is as follows:

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     public int val;
 *     public TreeNode left;
 *     public TreeNode right;
 *     public TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    
    
    public IList<IList<int>> BSTSequences(TreeNode root) {
    
    
        IList<IList<int>> ans = new List<IList<int>>();

        // 如果树为空,返回空
        if (root == null) {
    
     ans.Add(new List<int>()); return ans; }

        // BFS 遍历,将结果存储到 ans 中
        Partition(ans, new List<int>(), new List<TreeNode> {
    
     root });

        return ans;
    }

    // lst 存储每种情况的数组,next 存储下一个访问结点
    public void Partition(IList<IList<int>> ans, IList<int> lst, List<TreeNode> next) {
    
    
        // 遍历每个 next 结点
        for (int i = 0; i < next.Count; i++) {
    
    
            // 结点处理
            TreeNode node = next[i];                      // 取出该结点 node
            lst.Add(node.val);                            // 将 node 值加入 lst 中
            next.Remove(node);                            // 访问完成后删除 node 记录
            if (node.left != null) next.Add(node.left);   // 左右孩子不为空,则将成为下一个访问节点
            if (node.right != null) next.Add(node.right);

            Partition(ans, lst, next); // 继续访问后续结点

            // 访问完成后,回到原始状态,为进入下一个 next 结点做准备
            next.Insert(i, node);    // 收回 node 结点
            next.Remove(node.left);  // 删除 node 左右孩子的记录
            next.Remove(node.right);
            lst.Remove(node.val);    // 取出 node 值
        }

        if (next.Count == 0) ans.Add(new List<int>(lst)); // next 为空,表示遍历完所有结点,此时将 lst 放入 ans 中
                                                          // 注意需要拷贝 lst,否则加入的是引用
    }
}

  You can change the next array to a queue to reduce the time required to delete elements. I won’t change it here, I’m lazy.

  • Time complexity: difficult to calculate.
  • Space complexity: O ( n ) O(n)O ( n )

Guess you like

Origin blog.csdn.net/zheliku/article/details/132889986