3.12 一轮练习hot100(105. 从前序与中序遍历序列构造二叉树 96. 不同的二叉搜索树 )

题一:105. 从前序与中序遍历序列构造二叉树

3.12 一轮练习hot100(105. 从前序与中序遍历序列构造二叉树 96. 不同的二叉搜索树 )_第1张图片

链接

题目链接:
文章链接:

关键点

  1. 前序遍历和中序遍历的特点!!,前序提供了根节点信息,中序的根节点左边是左子树,右边是右子树。

编程思路

Me:

力扣实战

思路一:递归

# 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
class Solution:
    def buildTree(self, preorder: List[int], inorder: List[int]) -> Optional[TreeNode]:
        def recur_func(inorder):
            x = preorder.pop(0)  # 每次取前序列表最左端的元素,且pop之后,剩余的部分的开头都是root的左子树的前序,且第一个元素是根
            node = TreeNode(x)  # 用该元素生成一个node
            idx = inorder.index(x)  # 找到该元素在中序列表中的索引
            left_l = inorder[:idx]  # 用该元素分割中序列表
            right_l = inorder[idx+1:]
            node.left = recur_func(left_l) if left_l else None  
            node.right = recur_func(right_l) if right_l else None
            # 一直探索到最底层的最左端的叶子,然后从下往上一层层返回
            return node

        return recur_func(inorder)
        
# 反思1:

思路二:用栈模拟递归来迭代,估计有hard的难度

 

题二:114. 二叉树展开为链表

链接

题目链接:
文章链接:题解的四个方法很np,很多都是随想录曾有的

关键点

  1. 通过前序遍历(递归,用栈迭代),把每个节点存入列表
  2. 按照顺序连

编程思路

Me:

力扣实战

思路一:

1. 通过前序遍历(递归,用栈迭代),把每个节点存入列表
2. 按照顺序连
class Solution:
    def flatten(self, root: TreeNode) -> None:
        preorderList = list()

        def preorderTraversal(root: TreeNode):
            if root:
                preorderList.append(root)
                preorderTraversal(root.left)
                preorderTraversal(root.right)
        
        preorderTraversal(root)
        size = len(preorderList)
        for i in range(1, size):
            prev, curr = preorderList[i - 1], preorderList[i]
            prev.left = None
            prev.right = curr

 class Solution:
    def flatten(self, root: TreeNode) -> None:
        preorderList = list()
        stack = list()
        node = root

        while node or stack:
            while node:
                preorderList.append(node)
                stack.append(node)
                node = node.left
            node = stack.pop()
            node = node.right
        
        size = len(preorderList)
        for i in range(1, size):
            prev, curr = preorderList[i - 1], preorderList[i]
            prev.left = None
            prev.right = curr

       
# 反思1:

思路二:前序遍历和展开同时

class Solution:
    def flatten(self, root: TreeNode) -> None:
        if not root:
            return
        
        stack = [root]
        prev = None
        
        while stack:
            curr = stack.pop()
            if prev:
                prev.left = None
                prev.right = curr
            left, right = curr.left, curr.right
            if right:
                stack.append(right)
            if left:
                stack.append(left)
            prev = curr

 

思路三 前驱节点☆☆☆

class Solution:
    def flatten(self, root: TreeNode) -> None:
        curr = root
        while curr:
            if curr.left:
                predecessor = nxt = curr.left
                while predecessor.right:
                    predecessor = predecessor.right
                predecessor.right = curr.right
                curr.left = None
                curr.right = nxt
            curr = curr.right


题三:

题四:

你可能感兴趣的:(一轮复习,leetcode,算法,职场和发展)