Tree

  • post-order traversal

两种方法遍历方法

traverse的方法,返回值是void 自顶向下,从大到小

void traverse(int x, int y, int sum) {
    if (x == n) {
        if(sum < best) {
            best = sum;
        }
        return;
    }

    traverse(x + 1, y, sum + A[x][y]);
    traverse(x + 1, y + 1, sum + A[x][y]);
}

divide and conquer, 有返回值 自底向上,从小到大

int divideConquer(int x, int y) {
    if(x == n) {
        return 0;
    }

    return (A[x][y] + divideConquer(x + 1,y) + divideConquer(x + 1, y + 1));
}

results matching ""

    No results matching ""