aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJack Sangdahl <[email protected]>2024-12-24 18:12:01 -0700
committerJack Sangdahl <[email protected]>2024-12-24 18:12:01 -0700
commit25db68c01af00345a3525e4bcb35dca29ba5c3b3 (patch)
treeb0218b0926fdde8e49accfc3bc5e7d22765d6690
parent48388e0eb7bf87ecb40353794acee7a43557198f (diff)
downloadleetcode-25db68c01af00345a3525e4bcb35dca29ba5c3b3.tar.gz
leetcode-25db68c01af00345a3525e4bcb35dca29ba5c3b3.zip
chore: python treenode helpers
-rw-r--r--.gitignore1
-rw-r--r--leetcode.py23
2 files changed, 24 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
index 675c3ef..11a3bd5 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,2 +1,3 @@
a.out
.gdb_history
+__pycache__
diff --git a/leetcode.py b/leetcode.py
new file mode 100644
index 0000000..6dcb275
--- /dev/null
+++ b/leetcode.py
@@ -0,0 +1,23 @@
+class TreeNode(object):
+ def __init__(self, val=0, left=None, right=None):
+ self.val = val
+ self.left = left
+ self.right = right
+
+
+def list_to_tree(input):
+ if not input:
+ return None
+ root = TreeNode(input[0])
+ q, i = [root], 1
+ while i < len(input):
+ curr = q.pop(0)
+ if input[i] is not None:
+ curr.left = TreeNode(input[i])
+ q.append(curr.left)
+ i += 1
+ if i < len(input) and input[i] is not None:
+ curr.right = TreeNode(input[i])
+ q.append(curr.right)
+ i += 1
+ return root