A binary tree is a tree data structure in which each node has at most two children, referred to as the left child and the right child.
[Leetcode 105] Construct Binary Tree from Preorder and Inorder Traversal
Given two integer arrays preorder and inorder where preorder is the preorder traversal of a binary tree and inorder is the inorder traversal of the same tree, construct and return the binary tree.
# Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): # self.val = val # self.left = left # self.right = right classSolution: defbuildTree(self, preorder: List[int], inorder: List[int]) -> Optional[TreeNode]: iflen(preorder) == 0: returnNone
# get root node from preorder list rootVal = preorder[0] root = TreeNode(rootVal)
# divide inorder list to left and right subtree rootIdx = inorder.index(rootVal) leftInorder = inorder[:rootIdx] rightInorder = inorder[rootIdx + 1 :]
# find corresponding preorder left and right subtree leftPreorder = preorder[1 : len(leftInorder) + 1] rightPreorder = preorder[len(leftInorder) + 1 :]
# recursively construct left and right subtree root.left = self.buildTree(leftPreorder, leftInorder) root.right = self.buildTree(rightPreorder, rightInorder)
# return the root of tree return root
[Leetcode 106] Construct Binary Tree from Inorder and Postorder Traversal
Given two integer arrays inorder and postorder where inorder is the inorder traversal of a binary tree and postorder is the postorder traversal of the same tree, construct and return the binary tree.
# Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): # self.val = val # self.left = left # self.right = right classSolution: defbuildTree(self, inorder: List[int], postorder: List[int]) -> Optional[TreeNode]: # check if empty tree list iflen(inorder) == 0: returnNone
# find the root in postorder list and construct the root node rootVal = postorder[-1] root = TreeNode(rootVal)
# divide the inorder list to left and right subtree rootIdx = inorder.index(rootVal) leftInorder = inorder[:rootIdx] rightInorder = inorder[rootIdx + 1 :]
# recursively construct left and right subtree root.left = self.buildTree(leftInorder, leftPostorder) root.right = self.buildTree(rightInorder, rightPostorder)
# return the root of tree return root
[Leetcode 129] Sum Root to Leaf Numbers
You are given the root of a binary tree containing digits from 0 to 9 only.
Each root-to-leaf path in the tree represents a number.
For example, the root-to-leaf path 1 -> 2 -> 3 represents the number 123. Return the total sum of all root-to-leaf numbers. Test cases are generated so that the answer will fit in a 32-bit integer.
A leaf node is a node with no children.
Example 1:
1 2 3 4 5 6 7 8 9 10
1 / \ 2 3
Input: root = [1,2,3] Output: 25 Explanation: The root-to-leaf path 1->2 represents the number 12. The root-to-leaf path 1->3 represents the number 13. Therefore, sum = 12 + 13 = 25.
Constraints:
The number of nodes in the tree is in the range [1, 1000].
# Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): # self.val = val # self.left = left # self.right = right from collections import deque