简体   繁体   中英

Sending in-order traversal elements to array

private void inOrder(BSTNode root) {
        if (root.getElem().equals(null)) {
            return;
        }
        inOrder(root.getLChild());
        // print to array, increment 2
        inOrder(root.getRChild());
}

So I believe printing to the array occurs at the comment line, since that is usually where the current node is outputted, but not sure how to send the in-ordered elements into an array? If it needs an extra parameter or different return type to make it work, that's ok, I can adjust it since it's a helper method.

Two things :

  1. You've got to pass along the array and index to your method (signature change).
  2. There on you can simply add this line instead of the comment:

     array[index++]= root.getData(); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM