【leetcode刷题】二叉树搜索-深度优先搜索,三种方法实现

概念就不多说了,直接上代码。

0. TreeNode

# 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

1. Preorder Traversal

1.1 Preorder递归

要点:确定终止条件,不然容易造成无限循环

class Solution:
    def preorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
        """递归法"""
        res = []

        def traversal(cur):
            if not cur:  # 一定要写终止条件
                return
            res.append(cur.val)
            traversal(cur.left)
            traversal(cur.right)

        traversal(root)
        return res

1.2 Preorder用栈

要点:先右孩子进栈,后左孩子进栈,
这样出栈顺序才能是中、左、右。

class Solution:
    def preorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
        """用栈"""
        if not root:
            return []
        res = []
        stack = [root]
        while stack:
            cur = stack.pop()
            res.append(cur.val)
            if cur.right:
                stack.append(cur.right)
            if cur.left:
                stack.append(cur.left)
        return res

1.3 Preorder用空指针

要点:加入了空指针标记要处理的节点:
当遇到节点为空时,下一个则是要处理的节点;
当遇到的节点非空时,要进行push的操作。

class Solution:
    def preorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
        """指针法"""
        res = []
        stack = []
        if root:
            stack.append(root)
        else:
            return []
        while stack:
            cur = stack.pop()
            if cur != None:
                if cur.right:
                    stack.append(cur.right)
                if cur.left:
                    stack.append(cur.left)
                stack.append(cur)
                stack.append(None)

            else:
                cur = stack.pop()
                res.append(cur.val)
        return res

2. Inorder Traversal

2.1 Inorder 递归

class Solution:
    def inorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
        """递归法"""
        res = []
        def traversal(cur):
            if not cur:
                return
            traversal(cur.left)
            res.append(cur.val)
            traversal(cur.right)

        traversal(root)
        return res

2.2 Inorder 用栈

class Solution:
    def inorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
        """用栈法"""
        if not root:
            return []
        res = []
        stack = []
        cur = root
        while cur or stack:
            if cur:
                stack.append(cur)
                cur = cur.left
            else:
                cur = stack.pop()
                res.append(cur.val)
                cur = cur.right
        return res

2.3 Inorder 用空指针

class Solution:
    def inorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
        """空指针法"""
        if not root:
            return []
        res = []
        stack = [root]
        while stack:
            cur = stack.pop()
            if cur:
                if cur.right:
                    stack.append(cur.right)
                stack.append(cur)  #就是这两句
                stack.append(None)  #就是这两句
                if cur.left:
                    stack.append(cur.left)
            else:
                res.append(stack.pop().val)
        return res

猜你喜欢

转载自blog.csdn.net/D2Ooo/article/details/126708005
今日推荐