leetcode

Leetcode Solutions (0x6A73)
git clone https://git.pngu.org/leetcode
Log | Files | Refs | Submodules | README

commit 25db68c01af00345a3525e4bcb35dca29ba5c3b3
parent 48388e0eb7bf87ecb40353794acee7a43557198f
Author: Jack Sangdahl <jack@pngu.org>
Date:   Tue, 24 Dec 2024 18:12:01 -0700

chore: python treenode helpers

Diffstat:
M.gitignore | 1+
Aleetcode.py | 23+++++++++++++++++++++++
2 files changed, 24 insertions(+), 0 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -1,2 +1,3 @@ a.out .gdb_history +__pycache__ diff --git a/leetcode.py 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