flâneur — a map of the web's best reading

Explore - LeetCode

leetcode.com · saved by 1 readers

Before starting this article, please make sure you have a complete understanding of what a binary tree is and how it is represented in code, as well as a solid understanding of recursion. In this article, we'll talk about how to traverse binary trees. Tree traversal is how we access the elements of a tree, and thus is mandatory for solving tree problems. Recall that in the linked list chapter, we traversed a linked list using the following code: The above code starts at the head and visits each node to find the sum of all values in the linked list. For each node, there is a moment in the code execution where the head variable is referencing the node. We traverse by using the .next attribute. Traversing a binary tree follows the same idea. We start at the root and traverse by using the child pointers .left and .right. When traversing linked lists, we usually do it iteratively. With binary trees, we usually do it recursively. There are two main types of tree traversals. The first is call

Explore this link on the map →

saved by