← 返回 bytedance 的题目列表Binary Tree Traversal
类型:online_judge
Implement binary tree traversal algorithms. You need to write three functions to perform preorder, inorder, and postorder traversals. Each function takes the root node of a binary tree and returns the result of the respective traversal.
Input Format:
class Node:
def __init__(self, value=0, left=None, right=None):
self.value = value
self.left = left
self.right = right
Output Format:
Each function returns a list of the respective traversal results for the binary tree.
Example:
root = Node(1, Node(2), Node(3))
preorder_traversal(root) # Output: [1, 2, 3]
inorder_traversal(root) # Output: [2, 1, 3]
postorder_traversal(root) # Output: [2, 3, 1]
Constraints:
Number of nodes in the binary tree n: 0 <= n <= 1000
Example
Input
1
2 3