private void _DiagonalSum(node x, listnode ptr, int Isleft) { if (x == null) { return; } if (Isleft == 0) { if (ptr.previous == null) { var leftnode = new listnode(x.val); ptr.previous = leftnode; leftnode.next = ptr; _DiagonalSum(x.Left, leftnode, 0); _DiagonalSum(x.Right, leftnode, 1); } else { ptr.previous.val += x.val; _DiagonalSum(x.Left, ptr.previous, 0); _DiagonalSum(x.Right, ptr.previous, 1); } } else if (Isleft == 1) { ptr.val += x.val; _DiagonalSum(x.Left, ptr, 0); _DiagonalSum(x.Right, ptr, 1); } else { _DiagonalSum(x.Left, ptr, 0); _DiagonalSum(x.Right, ptr, 1); } }
private void _verticalsum(node x, listnode ptr, int Isleft) { if (x == null) { return; } if (Isleft == 0) { if (ptr.previous == null) { var leftnode = new listnode(x.val); ptr.previous = leftnode; leftnode.next = ptr; _verticalsum(x.Left, leftnode, 0); _verticalsum(x.Right, leftnode, 1); } else { ptr.previous.val += x.val; _verticalsum(x.Left, ptr.previous, 0); _verticalsum(x.Right, ptr.previous, 1); } } else { if (ptr.next == null) { var rightnode = new listnode(x.val); ptr.next = rightnode; rightnode.previous = ptr; _verticalsum(x.Left, rightnode, 0); _verticalsum(x.Right, rightnode, 1); } else { ptr.next.val += x.val; _verticalsum(x.Left, ptr.next, 0); _verticalsum(x.Right, ptr.next, 1); } } }
public listnode(int val) { this.val = val; this.next = null; this.previous = null; }
public void verticalsum() { this.listroot = new listnode(root.val); this._DiagonalSum(root, listroot, 2); }