坐标移动

两数之和

变态跳台阶

根据先序和中序,重建二叉树
'''
题目:
输入二叉树的先序遍历和中序遍历,重建二叉树。
思路:递归
'''
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None
class Solution:
def reConstructBinaryTree(self, pre, tin):
if not pre or not tin:
return None
if len(pre) != len(tin):
return None
root = pre[0]
rootNode = TreeNode(root)
pos = tin.index(pre[0])
tinLeft = tin[:pos]
tinRight = tin[pos + 1:]
preLeft = pre[1:pos + 1]
preRight = pre[pos + 1:]
leftNode = self.reConstructBinaryTree(preLeft, tinLeft)
rightNode = self.reConstructBinaryTree(preRight, tinRight)
rootNode.left = leftNode
rootNode.right = rightNode
return rootNode