leetcode 二叉树的遍历

leetcode 144.二叉树的前序遍历

class TreeNode(object):
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None
class Solution(object):
    # 递归-根左右
    def preorderTraversal(self, root):
        def helper(root):
            if not root: return
            res.append(root.val)
            helper(root.left)
            helper(root.right)
                
        res = []
        helper(root)
        return res
    
    # 迭代-根左右
    def preorderTraversal(self, root):
        if not root: return []
        res = []
        stack = []
        node = root
        while stack or node:
            while node:
                res.append(node.val)
                stack.append(node)
                node = node.left
            node = stack.pop()
            node = node.right
        return res

leetcode 94.二叉树的中序遍历

class TreeNode(object):
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None
class Solution(object):
    # 递归-左根右
    def inorderTraversal(self, root):
        def helper(root):
            if not root: return
            helper(root.left)
            res.append(root.val)
            helper(root.right)
                
        res = []
        helper(root)
        return res
    
    # 迭代-左根右
    def inorderTraversal(self, root):
        if not root: return []
        res = []
        stack = []
        node = root
        while stack or node:
            while node:
                stack.append(node)
                node = node.left
            node = stack.pop()
            res.append(node.val)
            node = node.right
        return res

leetcode 145.二叉树的后序遍历

class TreeNode(object):
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None
class Solution(object):
    # 递归-左右根
    def postorderTraversal(self, root):
        def helper(root):
            if not root: return
            helper(root.left)
            helper(root.right)
            res.append(root.val)
        res = []
        helper(root)
        return res

    # 迭代-左右根
    def postorderTraversal(self, root):
        if not root: return []
        res = []
        stack = []
        node = root
        while stack or node:
            while node:
                stack.append(node)
                node = node.left if node.left else node.right
            node = stack.pop()
            res.append(node.val)
            if stack and stack[-1].left==node:
                node = stack[-1].right
            else:
                node = None
        return res

leetcode 102.二叉树的层序遍历

class TreeNode(object):
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None
class Solution(object):
    # 递归
    def levelOrder(self, root):
        def helper(root, depth):
            if not root: return
            if len(res)==depth: res.append([])
            res[depth].append(root.val)
            helper(root.left, depth+1)
            helper(root.right, depth+1)
        if not root: return []
        res = []
        helper(root, 0)
        return res
    # 迭代
    def levelOrder(self, root):
        if not root: return []
        res, layer = [], [root]
        while layer:
            cur_val = []
            next_layer = []
            for node in layer:
                cur_val.append(node.val)
                if not node.left: next_layer.append(node.left)
                if not node.right: next_layer.append(node.right)
            res.append(cur_val)
            layer = next_layer
        return res

leetcode 103.二叉树的锯齿形层次遍历

class TreeNode(object):
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None
class Solution(object):
    # 递归
    def zigzagLevelOrder(self, root):
        def helper(root, depth):
            if not root: return
            if len(res)==depth: res.append([])
            if depth%2:
                res[depth].insert(0, root.val)
            else:
                res[depth].append(root.val)
            helper(root.left, depth+1)
            helper(root.right, depth+1)
        if not root: return []
        res = []
        helper(root, 0)
        return res

    # 迭代
    def zigzagLevelOrder(self, root):
        if not root: return []
        res, layer, depth = [], [root], 0
        while layer:
            cur_val = []
            next_layer = []
            for node in layer:
                cur_val.append(node.val)
                if node.left: next_layer.append(node.left)
                if node.right: next_layer.append(node.right)
            if depth%2:
                res.append(cur_val[::-1])
            else:
                res.append(cur_val)
            layer = next_layer
            depth += 1
        return res

leetcode 107.二叉树的层次遍历 II

class TreeNode(object):
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None
class Solution(object):
    def levelOrderBottom(self, root):
        if not root: return []
        res, layer = [], [root]
        while layer:
            cur_val = []
            next_layer = []
            for node in layer:
                cur_val.append(node.val)
                if node.left: next_layer.append(node.left)
                if node.right: next_layer.append(node.right)
            res.insert(0, cur_val)
            layer = next_layer
        return res
posted @ 2020-05-10 17:19  YTiing  阅读(386)  评论(0编辑  收藏  举报