Python版day14

144. 二叉树的前序遍历

给你二叉树的根节点 root ,返回它节点值的 前序 遍历。 

# 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 preorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
        if not root:
            return []
        left = self.preorderTraversal(root.left)
        right = self.preorderTraversal(root.right)
        return [root.val] + left + right

迭代法

class Solution:
    def preorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
        if not root:
            return []
        res = []
        stack = deque()
        stack.append(root)
        while stack:
            node = stack.pop()
            res.append(node.val)
            left = node.left
            right = node.right
            # 先右后左 这样出栈的时候才能
            # 先左后右
            if right:
                stack.append(right)     
            if left:
                stack.append(left)
    
        return res

 

 94. 二叉树的中序遍历

给定一个二叉树的根节点 root ,返回 它的 中序 遍历 。 

# 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 inorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
        if not root:
            return []
        left = self.inorderTraversal(root.left)
        right = self.inorderTraversal(root.right)
        return left + [root.val] + righ

 迭代法

class Solution:
    def inorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
        if not root:
            return []
        # 注意在这里不能提前将
        # root节点加入到stack中
        stack = deque() 
        result = []
        curr = root
        while curr or stack:
            # 先迭代访问最底层左子树的节点
            if curr:
                stack.append(curr)
                curr = curr.left # 左
            # 到达最左节点后处理栈顶结点                
            else:
                curr = stack.pop()
                # 把左节点/中节点添加到result中
                # 如果添加的是左节点那么下一个就是中节点
                # 如果添加的是中节点那么下一个就是右节点
                result.append(curr.val)
                # 取栈顶右边节点
                curr = curr.right
        return result

 145. 二叉树的后序遍历

给你一棵二叉树的根节点 root ,返回其节点值的 后序遍历 

# 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 postorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
        if not root:
            return []
        left = self.postorderTraversal(root.left)
        right = self.postorderTraversal(root.right)
        return left + right + [root.val]

 迭代法

class Solution:
    # 前序遍历是中左右
    # 调整代码左右顺序 变成 中右左
    #反转result数组变成 左右中
    def postorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
        if not root:
            return []
        stack = deque()
        res = []
        stack.append(root)
        while stack:
            node = stack.pop()
            res.append(node.val)
            if node.left:
                stack.append(node.left)
            if node.right:
                stack.append(node.right)
        res.reverse()
        return res

你可能感兴趣的:(数学建模,算法)