TreeNode bt = build("");
TreeNode build(String data) {
TreeNode res = null;
String[] s = data.split(",");
int len = s.length;
if (len > 0) {
TreeNode[] a = new TreeNode[len];
for (int i = 0; i < len; i++) {
if (!s[i].equals("#")) {
a[i] = new TreeNode(Integer.parseInt(s[i]));
}
}
int preNull = 0;
for (int i = 0; i < len; i++) {
if (a[i] == null) {
preNull++;
} else {
int leftChilIndex = 2 * i + 1 - preNull * 2;
if (leftChilIndex >= len) {
break;
}
a[i].left = a[leftChilIndex];
int rightChildIndex = 2 * i + 2 - preNull * 2;
if (rightChildIndex >= len) {
break;
}
a[i].right = a[rightChildIndex];
}
}
res = a[0];
}
return res;
}
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int v) { val = v; }
}
ListNode head = build(true, false, "");
ListNode build(boolean useDummyHead, boolean usePre, String data) {
String[] ints = data.split(",");
ListNode h = useDummyHead ? new ListNode(Integer.MIN_VALUE) : null;
ListNode t = useDummyHead ? h : null;
for (String i: ints) {
ListNode n = new ListNode(Integer.parseInt(i));
if (h == null) {
h = n;
} else {
t.next = n;
if(usePre) {
n.pre = t;
}
}
t = n;
}
return h;
}
class ListNode {
int val;
ListNode next, pre;
ListNode(int v) {
val = v;
next = null;
pre = null;
}
}
TreeNode * bt = build("");
TreeNode * build(string data) {
TreeNode * res = NULL;
stringstream temp(data);
string seg;
vector < TreeNode * > a;
while (getline(temp, seg, ',')) {
if (seg != "#") {
a.push_back(new TreeNode(stoi(seg)));
} else {
a.push_back(NULL);
}
}
int len = a.size();
if (len > 0) {
int preNull = 0;
for (int i = 0; i < len; i++) {
TreeNode * cur = a[i];
if (cur) {
int leftChilIndex = 2 * i + 1 - preNull * 2;
if (leftChilIndex >= len) {
break;
}
cur -> left = a[leftChilIndex];
int rightChildIndex = 2 * i + 2 - preNull * 2;
if (rightChildIndex >= len) {
break;
}
cur -> right = a[rightChildIndex];
} else {
preNull++;
}
}
res = a[0];
}
return res;
}
struct TreeNode {
int val;
TreeNode * left;
TreeNode * right;
TreeNode(int v) : val(v), left(NULL), right(NULL) {}
};
ListNode * h = build(true, false, "");
ListNode * build(bool useDummyHead, bool usePre, string data) {
ListNode * h = useDummyHead ? new ListNode(-999) : NULL;
ListNode * t = useDummyHead ? h : NULL;
stringstream temp(data);
string seg;
while (getline(temp, seg, ',')) {
ListNode * n = new ListNode(stoi(seg));
if (h) {
t -> next = n;
if (usePre) {
n -> pre = t;
}
} else {
h = n;
}
t = n;
}
return h;
}
struct ListNode {
int val;
ListNode * next;
ListNode * pre;
ListNode(int v): val(v), next(NULL), pre(NULL) {}
};
# Note: This line must execute after all definitions below.
bt = build("")
def build(data):
if len(data) == 0:
return None
s = data.split(",")
l = len(s)
a = []
for val in s:
if val != "#":
a.append(TreeNode(int(val)))
else:
a.append(None)
preNull = 0
for i, val in enumerate(a):
if val is None:
preNull += 1
else:
left = 2 * i + 1 - preNull * 2
if left >= l:
break;
a[i].left = a[left]
right = 2 * i + 2 - preNull * 2
if right >= l:
break;
a[i].right = a[right]
return a[0]
class TreeNode(object):
def __init__(self, v):
self.val = v
self.left = None
self.right = None
# Note: This line must execute after all definitions below.
bt = build(True, False, "")
def build(useDummyHead, usePre, data):
if len(data) == 0:
return None
h = ListNode(-999) if useDummyHead else None
t = h if useDummyHead else None
s = data.split(",")
for val in s:
n = ListNode(int(val))
if h is None:
h = n
else:
t.next = n
if usePre:
n.pre = t
t = n
return h
class ListNode(object):
def __init__(self, v):
self.val = v
self.next = None
self.pre = None