This post is about preorder traversal of a binary tree, a method of traversing the tree in which the current node is visited before its children. The preorder traversal algorithm visits the root node first, then recursively visits the left subtree, followed by the right subtree. The result of a preorder traversal is a list of the nodes' values in the order they are visited. This post will help you to understand how to traverse a binary tree and also how to implement it in different ways.
Preorder traversal is a method of traversing a binary tree where the current node is visited before its children. The algorithm visits the root node first, then recursively visits the left subtree, followed by the right subtree. The result of a preorder traversal is a list of the nodes' values in the order they are visited.
In this problem, you are given the root of a binary tree represented by the TreeNode
class in Kotlin, which has a val
property for the node's value and left
and right
properties for its left and right children, respectively. Your task is to write two implementations of a function that performs a preorder traversal of the binary tree and returns a list of the nodes' values. One should be a recursive solution and the other should not use recursion.
Here is the class signature of the function:
fun preorderTraversal(root: TreeNode?): List<Int>
The input to the function is the root node of the binary tree, and the output is a list of integers representing the values of the nodes in the order they are visited during the preorder traversal.
Given the root of a binary tree, return the preorder traversal of its nodes' values. Do this both with a recursive solution and without using recursion.
class TreeNode(var `val`: Int) {
var left: TreeNode? = null
var right: TreeNode? = null
}
Here is a recursive solution to the problem:
fun preorderTraversal(root: TreeNode?): List<Int> {
val result = mutableListOf<Int>()
if (root == null) return result
result.add(root.val)
result.addAll(preorderTraversal(root.left))
result.addAll(preorderTraversal(root.right))
return result
}
This function does a preorder traversal of the binary tree and returns a list of the nodes' values in the order that they are visited.
This algorithm has a time complexity of O(n), where n is the number of nodes in the tree, and a space complexity of O(h), where h is the height of the tree (due to the call stack).
fun preorderTraversal(root: TreeNode?): List<Int> {
val result = mutableListOf<Int>()
val stack = Stack<TreeNode>()
var current = root
while (current != null || stack.isNotEmpty()) {
while (current != null) {
result.add(current.val)
stack.push(current)
current = current.left
}
current = stack.pop()
current = current.right
}
return result
}
This function does a preorder traversal of the binary tree and returns a list of the nodes' values in the order that they are visited. It uses a stack to keep track of the nodes that still need to be visited.
The algorithm works as follows:
Initialize an empty stack and set the current node to the root of the tree.
While the current node is not null or the stack is not empty:
While the current node is not null:
Pop the top node from the stack and set the current node to its right child.
Return the result list.
This algorithm has a time complexity of O(n), where n is the number of nodes in the tree, and a space complexity of O(h), where h is the height of the tree.
I hope this helps! Let me know if you have any other questions.