Breaking out of a recursion in java

后端 未结 11 1442
故里飘歌
故里飘歌 2020-12-01 04:23

The recursion is sort of a \'divide and conquer\' style, it splits up while getting smaller (Tree data structure), and I want it to break completely if a violation is found,

11条回答
  •  时光取名叫无心
    2020-12-01 05:01

    You could return an error code, or modify some global variable so that each recursive instance knows to "kill itself".

    Something of the sort.

    int foo(bar){
         int to_the_next;
    
          if (go_recursive){
                to_the_next = foo(whisky_bar);
    
                if (to_the_next ==DIE) return DIE;
          }
    
          if (something_unexpected_happened) return DIE;
    
          process;//may include some other recursive calls, etc etc
    }
    

提交回复
热议问题