python leetcode Binary Tree Traversal

本文详细介绍了二叉树的遍历算法,包括前序、中序、后序及层次遍历等方法,提供了递归和非递归实现方式,并展示了如何利用栈和队列进行遍历,适用于算法学习和面试准备。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Binary Tree Inorder Traversal(递归,非递归)还有神级方法(使用常数级空间)

class Solution(object):
    def inorderTraversal(self, root):
        """
        :type root: TreeNode
        :rtype: List[int]
        """
        if root==None:
            return []
        return self.inorderTraversal(root.left)+[root.val]+self.inorderTraversal(root.right)
class Solution:
    def inorderTraversal(self, root):
        """
        :type root: TreeNode
        :rtype: List[int]
        """
        if root == None:
            return []
        stack, res, node = [], [], root
        while len(stack)>0 or node != None:
            while node != None:
                stack.append(node)
                node = node.left
            node = stack.pop()
            res.append(node.val)
            node = node.right 
        return res
class Solution(object):
    def inorderTraversal(self, root):
        """
        :type root: TreeNode
        :rtype: List[int]
        """
        if root==None:
            return []
        cur1=root
        cur2=None
        res=[]
        while cur1:
            cur2=cur1.left
            if cur2:
                while cur2.right and cur2.right!=cur1:
                    cur2=cur2.right
                if not cur2.right:
                    cur2.right=cur1
                    cur1=cur1.left
                    continue 
                else:
                    cur2.right=None
            res.append(cur1.val)
            cur1=cur1.right
        return res

Binary Tree Preorder Traversal

class Solution:
    def preorderTraversal(self, root):
        """
        :type root: TreeNode
        :rtype: List[int]
        """
        if not root: return []
        return [root.val]+self.preorderTraversal(root.left)+self.preorderTraversal(root.right)
class Solution(object):
    def preorderTraversal(self, root):
        """
        :type root: TreeNode
        :rtype: List[int]
        """
        if not root:
            return []
        stack = [root]
        res=[]
        while stack:
            node = stack.pop()
            res.append(node.val)
            if node.right:
                stack.append(node.right)
            if node.left:
                stack.append(node.left)
            
        return res
class Solution:
    def preorderTraversal(self, root):
        """
        :type root: TreeNode
        :rtype: List[int]
        """
        if not root: return []
        cur1=root
        cur2=None
        res=[]
        while cur1:
            cur2=cur1.left
            if cur2:
                while cur2.right and cur2.right!=cur1:
                    cur2=cur2.right 
                if not cur2.right:
                    cur2.right=cur1
                    res.append(cur1.val)
                    cur1=cur1.left
                    continue 
                else:
                    cur2.right=None 
            else:
                res.append(cur1.val)
            cur1=cur1.right 
        return res

Binary Tree Postorder Traversal

class Solution:
    def postorderTraversal(self, root):
        """
        :type root: TreeNode
        :rtype: List[int]
        """
        if not root: return []
        return self.postorderTraversal(root.left)+self.postorderTraversal(root.right)+[root.val]
class Solution(object):
    def postorderTraversal(self, root):
        """
        :type root: TreeNode
        :rtype: List[int]
        """
        if root==None:
            return []
        stack=[root]
        res=[]
        h=root
        c=None
        while stack:
            c=stack[-1]
            if c.left and c.left!=h and c.right != h:
                stack.append(c.left)
            elif c.right and c.right != h:
                stack.append(c.right)
            else:
                node = stack.pop()
                res.append(node.val)
                h=node
        return res

103. Binary Tree Zigzag Level Order Traversal

class Solution:
    def zigzagLevelOrder(self, root):
        """
        :type root: TreeNode
        :rtype: List[List[int]]
        """
        if not root: return []
        queue=[root]
        F=True
        res=[]
        while queue:
            if F:
                res.append([n.val for n in queue])
            else:
                res.append([queue[i].val for i in range(len(queue)-1,-1,-1)])
            tmp=[]
            for n in queue:
                if n.left: tmp.append(n.left)
                if n.right: tmp.append(n.right)
            queue=tmp
            F=not F 
        return res

Binary Tree Level Order Traversal II

class Solution(object):
    def levelOrderBottom(self, root):
        """
        :type root: TreeNode
        :rtype: List[List[int]]
        """
        res=[]
        if not root:
            return res 
        stack=[root]
        while stack:
            stack1=[]
            tmp=[]
            while stack:
                node=stack.pop()
                tmp.append(node.val)
                stack1.append(node)
            res.append(tmp)
            while stack1:
                node=stack1.pop()
                if node.right:
                    stack.append(node.right)
                if node.left:
                    stack.append(node.left)
        res.reverse()
        return res
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值