diff options
author | Jack Sangdahl <[email protected]> | 2024-12-24 18:12:01 -0700 |
---|---|---|
committer | Jack Sangdahl <[email protected]> | 2024-12-24 18:12:01 -0700 |
commit | 25db68c01af00345a3525e4bcb35dca29ba5c3b3 (patch) | |
tree | b0218b0926fdde8e49accfc3bc5e7d22765d6690 | |
parent | 48388e0eb7bf87ecb40353794acee7a43557198f (diff) | |
download | leetcode-25db68c01af00345a3525e4bcb35dca29ba5c3b3.tar.gz leetcode-25db68c01af00345a3525e4bcb35dca29ba5c3b3.zip |
chore: python treenode helpers
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | leetcode.py | 23 |
2 files changed, 24 insertions, 0 deletions
@@ -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 |