Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ...
- n = a.length + 1;
- P = new int[n][n];
- W = new int[n][n];
- r = new int[n][n];
- // ...
- weightedPathLength = P[0][n - 1];
- root = new Node(r[0][n - 1]);
- reconstructTree(0, n - 1, root);
- }
- private void reconstructTree(int i, int j, Node parent) {
- int l = r[i][j];
- Node child;
- if (l - i - 1 > 0) {
- child = new Node(r[i][l - 1]);
- parent.left = child;
- reconstructTree(i, l - 1, child);
- }
- if (j - l > 0) {
- child = new Node(r[l][j]);
- parent.right = child;
- reconstructTree(l, j, child);
- }
- }
Add Comment
Please, Sign In to add comment