递归代码模版

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# python 递归模版
def recursion(level, param1, param2, ...):
# recursion terminator
if level > MAX_LEVEL:
process_result
return

# process logic in current level
process(level, data...)

# drill down
self.recursion(level+1, p1, ...)

# reverse the current level status if needed

// Java递归模版
public class recursionTemplate {
public void recur(int level, int param) {

    // terminator
    if (level > MAX_LEVEL) {
        // process result
        return;
    }

    // process current logic
    process(level, param);

    // drill down
    recur(level: level + 1, newParam);

    // restore current status
}

}