This article involves knowledge points
The basic concept of binary tree
Stack usage
The basic concepts of the binary tree and the related concepts of the stack have been introduced before, and the forgotten friends will definitely double the effect after reviewing!
Review of Binomial Tree Knowledge: [ Today, add a BGM to the Binomial Tree, the Binomial Tree is singing! ]
Stack knowledge review: [leetcode stack queue] 1 Stack implementation queue
1Leetcode94 In-order traversal of binary tree
Given a binary tree, return its mid-order traversal.
Example 1:
[1,null,2,3] 1 \ 2 / 3 [1,3,2]
Xiaolan hopes everyone will think about it for 1 minute
The effect is better!
0 1 Question analysisIdeas
The basic idea
For a binary tree, we can get the root pointer of the root node, the first to visit is the root node. But we need to output in the order of the left subtree, the root node, and the right subtree, so what data structure has the characteristics of appearing first, which introduces the stack .
Visit from the root node, visit its left node in turn, and stack.
If it is NULL, pop the top element of the stack and put the right node of this element on the stack, repeat this step. As shown in the figure above, D has no left and right nodes, and the top D and B of the stack are popped up. At this time, if there is a right node in B, it will be pushed into the stack as shown in the figure below.
1c++ version 2python version 3java version