You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

820 B

将树转化为数组

  • static List<Integer> kminArray(TreeNode root){
      List<Integer> array=new ArrayList<>();
      if(root==null){
          return array;
      }
      //利用递归法,将树转化为数组
      array.addAll(kminArray(root.left));//牛
      array.add(root.val);
      array.addAll(kminArray(root.right));
      return array;
    
    }

树的创建方法-队列

  • static TreeNode createTree(int rootIndex, Integer[] values) { if (rootIndex >= values.length) { return null; } if (values[rootIndex] == null) { return null; } TreeNode rootNode = new TreeNode(); rootNode.val = values[rootIndex]; rootNode.left = createTree(2 * rootIndex + 1, values); rootNode.right = createTree(2 * rootIndex + 2, values); return rootNode;//返回根节点 }