有没有办法将二进制转换为排序数组,而不必为每个数组索引遍历树?
Node root;
Node runner;
int current_smallest;
void findsmallest(Node root){
//Pre-order traversal
if(root == null){
return;
}else{
runner = root;
if(current_smallest == null){
current_smallest = runner.element;
}else{
if(current_smallest > runner.element){
current_smallest = runner.element;
}
}
findsmallest(runner.left);
findsmallest(runner.right);
}
}
void fill_array( int [] array ){
for(int i =0; i < array.length(); i++){
findsmallest(root);
array[i] = current_smallest;
}
}
如您所见,如果树中有很多节点,这可能需要很长时间。顺便说一句,我忘了表明必须在开始时遍历整个树才能获得数组的长度。