파비의 매일매일 공부기록

Today's Challenge 본문

Problem Solving/LeetCode

Today's Challenge

fabichoi 2023. 1. 9. 23:45

 

class Solution:
    def preorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
        if not root:
            return []
        st, res = [root], []

        while st:
            node = st.pop()
            res.append(node.val)

            if node.right:
                st.append(node.right)
            if node.left:
                st.append(node.left)
        
        return res

https://leetcode.com/problems/binary-tree-preorder-traversal/

 

Binary Tree Preorder Traversal - LeetCode

Binary Tree Preorder Traversal - Given the root of a binary tree, return the preorder traversal of its nodes' values.   Example 1: [https://assets.leetcode.com/uploads/2020/09/15/inorder_1.jpg] Input: root = [1,null,2,3] Output: [1,2,3] Example 2: Input:

leetcode.com

매우 기초적인 순회 문제쓰~

 

반응형

'Problem Solving > LeetCode' 카테고리의 다른 글

Today's Challenge  (0) 2023.01.11
Today's Challenge  (0) 2023.01.10
Today's Challenge  (0) 2023.01.08
Today's Challenge  (0) 2023.01.07
Today's Challenge  (0) 2023.01.06
Comments