【问题标题】:Convert Sorted Array to Binary Search Tree [duplicate]将排序数组转换为二叉搜索树
【发布时间】:2017-07-12 13:31:23
【问题描述】:

我正在研究“将排序数组转换为具有最小高度的二叉搜索树”,它问:

给定一个已排序(递增顺序)的数组,将其转换为具有最小高度的二叉树。

我无法找到我的递归没有按预期停止的原因。它应该在 7 过去时停止,并且不会再次打印出 7。我也找到了一个类似的答案,看起来和我的策略一样,但效果很好。 (我不认为我的问题与上面列出的那些问题重复,但我仍然要感谢您为我链接它们。他们给了我更多的想法来解决我的问题。)

我的代码如下:

public TreeNode sortedArrayToBST(int[] A) {  
    int len = A.length;
    if(len <= 0){
        return null;
    }

    TreeNode root = new TreeNode(A[(len - 1) / 2]);
    if(len == 1){
        return root;
    }
    else{
        helper(root, A, 0, len - 1);
    }
    return root;
}

public void helper(TreeNode root, int[] A, int leftPoint, int rightPoint){
    if((rightPoint - leftPoint) <= 0){
        return;
    }

    int mid = (rightPoint - leftPoint) / 2 + leftPoint;
    int leftChild = (mid - 1 - leftPoint) / 2 + leftPoint;
    int rightChild = (rightPoint - (mid + 1)) / 2 + mid + 1;

    TreeNode left = new TreeNode(A[leftChild]);
    root.left = left;

    TreeNode right = new TreeNode(A[rightChild]);
    root.right = right;

    helper(root.left, A, leftPoint, mid - 1);
    helper(root.right, A, mid + 1, rightPoint);
    return;
}

当我运行它时,我得到了这个。

我的意见
[1,2,3,4,5,6,7,8]

我的输出
{4,2,6,1,3,5,7,#,#,#,#,#,#,7,8}

预期
{4,2,6,1,3,5,7,#,#,#,#,#,#,#,8}

为什么右边有重复的7?由于 7 已被使用,因此应将其踢出。

我发现我的想法与以下答案相似:

public TreeNode sortedArrayToBST(int[] A) {  
    // write your code here
    int len = A.length;
    if(len <= 0){
        return null;
    }
    TreeNode root = helper1(A, 0, len - 1);
    return root;
}

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

【问题讨论】:

  • 你试过调试吗?您最有可能发现问题。
  • 我试过调试,我也在纸上运行它。我现在肯定很困惑......
  • 类似的答案不是如你所愿吗? @X.Amanda
  • 类似的答案有效,但我的无效。我想知道为什么...
  • 请将解决方案作为答案发布,而不是作为问题的更新。这是为了避免混淆。我很欣赏它已被标记为重复,但请不要在问题中添加解决方案。谢谢。

标签: java algorithm binary-tree


【解决方案1】:

让我们有以下数组:

[1,2,3,4,5,6,7]

预期的 BST 是:

       4
    2     6
  1   3  5  7

为此,我们可以采取以下方式:

for (int i = 0; i < logn; i++) {
    //insert ith level
}

为了方便起见,让我们找到 min n,所以 n &gt; array.lengthn = 2^k

在第 i 层,从i = 0 开始,我们得到:

n/2^(i+1), 3*n/2^(i+1), 5*n/2^(i+1)...

上面的数字,都是数组中的索引。

public TreeNode sortedArrayToBST(int[] A) {  
    int len = A.length;
    if(len <= 0){
        return null;
    }

    int n = 1;
    int i = 0;
    while (n < len) {
        n *= 2;
        i++;
    }

    TreeNode root = new TreeNode(A[n/2]);

    for (int j = 1; j < i; j++) {
        insert(root, j, n, A);
    }
}

private void insert(TreeNode root, int j, int n, int[] A) {

    int helper = n/Math.pow(2, j+1);
    for (int i = 1; i <= Math.pow(2, j); i ++) {
        root.add(A[i*helper]);
    }
}

【讨论】:

  • 什么是root.add()?
  • 添加新节点的方法。我不知道你怎么称呼它
【解决方案2】:

这可能有效

public TreeNode sortedArrayToBST(int[] A) {
    if (A.length() == 0)
        return null;

    return helper1(A, 0, A.length - 1);
}
public TreeNode helper1(int[] A, int low, int high) {
    if (low > high)
        return null;

    // Binary Search
    int mid = low + (high - low)/2;
    TreeNode root = new TreeNode(A[mid]);

    root.left = helper1(A, low, mid - 1);
    root.right = helper1(A, mid + 1, high);

    return root;
}

【讨论】:

    猜你喜欢
    • 2020-12-28
    • 2018-11-05
    • 2013-11-13
    • 1970-01-01
    • 2013-04-17
    • 1970-01-01
    • 2014-12-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多