### Title:

Given a binary tree, return its mid-order traversal.

Given a binary tree, return the inorder traversal of its nodes' values.

Example:

```Input: [1,null,2,3]
1
\
2
/
3

Output: [1, 3, 2]```

Advanced: Recursive algorithm is very simple. Can you complete it by iterative algorithm?

Follow up: Recursive solution is trivial, could you do it iteratively?

### Ideas for solving problems:

Encyclopedia: two sequential tree traversal: https://baike.baidu.com/item/ordinal traversal

Traversal order: left subnode - > root node - > right subnode

The binary tree as shown below:

``````       A
/   \
B       C
/  \     /  \
D    E   F    G``````

The traversal order is D-> B-> E-> A-> F-> C-> G

Binary tree traversal can be accomplished by DFS (depth first search), which can be realized by recursion or iteration with data structure stack.

This traversal mode is essentially a stack traversal mode of "FIFO". In fact, the recursive method also realizes the stack "FIFO" in a recursive way.

### DFS-Recursion:

Java:

``````class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> list = new ArrayList<>();//array
dfs_recursion(root, list);//Afferent recursive function
return list;
}

private void dfs_recursion(TreeNode node, List<Integer> list) {
if (node == null) return;//Baseline condition
dfs_recursion(node.left, list);//Traversing the left child node first
list.add(node.val);//Traverse to the vertex of the left sub-node to get the value of the node
dfs_recursion(node.right, list);//Recursive traversal of right node
}
}``````

Python3:

``````class Solution:
def inorderTraversal(self, root: TreeNode) -> List[int]:
#array
res = list()
#Afferent recursive function
self.dfs_recursion(root, res)
return res

def dfs_recursion(self, node: TreeNode, res: List[int]):
#Baseline condition
if not node: return
#Recursive traversal of left child nodes
self.dfs_recursion(node.left, res)
#Take the value of the vertex node
res.append(node.val)
#Recursive traversal of right child nodes
self.dfs_recursion(node.right, res)``````

### DFS-Iteration:

Java:

``````class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> list = new ArrayList<>();//array
if (root == null) return list;
Stack<TreeNode> stack = new Stack<>();//Storing nodes with data structure stack
TreeNode cur = root;//Define current node
while (!stack.isEmpty() || cur != null) {//Loop condition: the stack is not empty or the current node is not empty
if (cur != null) {//The current node is not empty
stack.push(cur);//The current node is stacked
cur = cur.left;//Refresh the current node
} else {//Current node space-time
cur = stack.pop();//The parent node of the current node goes out of the stack
list.add(cur.val);//Values of arrays stored in nodes
cur = cur.right;Refresh the current node
}
}
return list;
}
}``````

Python:

``````class Solution:
def inorderTraversal(self, root: TreeNode) -> List[int]:
#Initialize arrays, stacks
res, stack = list(), list()
#The current node points to the root node
cur = root
#The recursive condition is: stack or current node is not empty
while stack or cur:
if cur:
#Current nodes are stacked in non-space-time
stack.append(cur)
#Refresh the current node
cur = cur.left
else:
#When the current node is empty, its parent node goes out of the stack
cur = stack.pop()
#Its value is stored in an array
res.append(cur.val)
#Refresh the current node
cur =cur.right
return res``````

Learning acridine together, welcome to pay attention: love to write Bug