leetcode递增顺序搜索树,检测到循环错误

ecbunoof  于 2021-06-30  发布在  Java
关注(0)|答案(0)|浏览(152)

所以我对leetcode还不熟悉,但是在将二叉树重新配置为右节点下降,左节点总是空的顺序时,我似乎遇到了这个循环错误。
这是我的密码

/* Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */

class Solution {

    public TreeNode temp, head;

    public TreeNode increasingBST(TreeNode root) {
        return createArrayList(root);
    }

    public TreeNode createArrayList(TreeNode root){
        if(root == null){
            head = root;
            return head;
        }
        if(root.left != null){
            createArrayList(root.left);
        }
        if (temp == null){
            temp = root;
            head = temp;
        }else{
            System.out.println("Temp: " + temp.val);
            System.out.println("root: " + root.val);
            temp.right = root;
            System.out.println("TempR: " + temp.right.val);
            temp.left = null;
            temp = temp.right;
            System.out.println("NewTemp: " + temp.val);
        }
        if(root.right != null){
            createArrayList(root.right);
        }

        return head;
    }
}

我通过了36/37的测试,但出于某种原因,它说最后一个测试有一个周期。有人能找到吗。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题