title: Build a binary tree from a Python list
body: <p>Let's say I have the following binary tree:</p>
  <p><a href="https://i.stack.imgur.com/7J0U6.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/7J0U6.png" alt="enter image description here" /></a></p>
  <p>and the following <code>TreeNode</code> definition:</p>
  <pre><code>class TreeNode:
      def __init__(self, x, left: = None, right: = None):
          self.val = x
          self.left = left
          self.right = right
  </code></pre>
  <p>To construct this tree, I currently need to write the following code:</p>
  <pre><code>def build_tree() -&gt; TreeNode:
      node0 = TreeNode(2, left=TreeNode(4), right=TreeNode(5))
      node1 = TreeNode(3, right=TreeNode(7))
      root = TreeNode(1, left=node0, right=node1)
      return root
  </code></pre>
  <p>This is very inefficient for large trees. Leetcode gives me trees as a Python list. For the example above, it reads:</p>
  <pre><code>[1, 2, 3, 4, 5, None, 7]
  </code></pre>
  <p>I would love to refactor <code>build_tree</code> so it takes a list, builds the tree and returns the root node:</p>
  <pre><code>def build_tree(values: list) -&gt; ListNode:
      ...
  </code></pre>
  <p>Any ideas on how to create this function?</p>
  <p>Here's a more complicated example:</p>
  <pre><code>[4, 6, 2, None, 5, 1, 9, 2, 1, 3, 4, None, 7]
  </code></pre>
  <p><a href="https://i.stack.imgur.com/PCqgN.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/PCqgN.png" alt="enter image description here" /></a></p>