## # 94. Binary Tree Inorder 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]
``````

### # Solution

Approach 1: recursion.

Approach 2: stack based -- push horizontal. We push the middle node (plus the right subtree) to stack because the middle node needs to be visited later.

Approach 3: Morris traversal -- construct a (partly) threaded binary tree, where each leaf node's right pointer points to its inorder successor (and deleting these pseudolinks later). Running time: O(N) -- see Binary Tree Preorder Traversal.

### # Code (Python)

Approach 1:

``````# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution:
def inorderTraversal(self, root):
"""
:type root: TreeNode
:rtype: List[int]
"""
# recursive
if not root:
return []
return self.inorderTraversal(root.left) + [root.val] + self.inorderTraversal(root.right)
``````

Approach 2:

``````    def inorderTraversal2(self, root):
# iterative -- push horizontal
# we can't start with stack = [root] because the moment you pop that node out it needs to be visited as the middle node.
result = []
stack = []
node = root
while node:
stack.append(node)
node = node.left
while stack:
node = stack.pop()
result.append(node.val)
node = node.right
while node:
stack.append(node)
node = node.left
return result
``````

Approach 3:

``````    def inorderTraversal(self, root):
# Morris traversal: https://leetcode.com/problems/binary-tree-inorder-traversal/discuss/148939/CPP-Morris-Traversal
# idea: construct a (partly) threaded binary tree, where each leaf node's right pointer points to its inorder successor
if not root:
return []
result = []
node = root
while node:
if not node.left: # happens either when a node doesn't have a left child, or a leaf node having a pseudolink
result.append(node.val)
node = node.right
else:
# for this node, search for the rightmost leaf on its left subtree (i.e. it's inorder predecessor)
prev = node.left
while prev.right and prev.right != node:
prev = prev.right
if not prev.right:
prev.right = node # set the pseudolink
node = node.left # dive down
else:
prev.right = None # remove pseudolink
result.append(node.val)
node = node.right
return result
``````

### # Code (C++)

Approach 1:

``````/**
* Definition for a binary tree node.
* struct TreeNode {
*     int val;
*     TreeNode *left;
*     TreeNode *right;
*     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
// Recursion.
class Solution {
private:
vector<int> inorder;
void doInorderTraversal(TreeNode* root) {
if (root == NULL)
return;
doInorderTraversal(root->left);
inorder.push_back(root->val);
doInorderTraversal(root->right);
}
public:
vector<int> inorderTraversal(TreeNode* root) {
doInorderTraversal(root);
return inorder;
}
};
``````

Approach 2:

``````// Iteration.
class Solution {
public:
vector<int> inorderTraversal(TreeNode* root) {
vector<int> inorder;
stack<TreeNode*> st;
TreeNode *node = root;
while (node)
{
st.push(node);
node = node->left;
}
while (!st.empty())
{
node = st.top();
st.pop();
inorder.push_back(node->val);
node = node->right;
while (node)
{
st.push(node);
node = node->left;
}
}
return inorder;
}
};
class Solution {
public:
vector<int> inorderTraversal(TreeNode* root) {
vector<int> inorder;
stack<TreeNode*> st;
TreeNode *p = root;
while (p || !st.empty())
{
if (p)
{
st.push(p);
p = p->left;
}
else
{
// Here st will not be empty.
p = st.top();
st.pop();
inorder.push_back(p->val);
p = p->right;
}
}
return inorder;
}
};
class Solution {
public:
vector<int> inorderTraversal(TreeNode* root) {
vector<int> inorder;
stack<TreeNode*> st;
if (root)
st.push(root);
while (!st.empty())
{
TreeNode *curr = st.top();
if (curr->left)
{
TreeNode *left = curr->left;
st.push(left);
curr->left = NULL; // avoid infinite loops.
curr = left;
}
else
{
inorder.push_back(curr->val);
st.pop();
if (curr->right)
st.push(curr->right);
}
}
return inorder;
}
};
``````

Approach 3:

``````// Morris Traversal.
class Solution {
public:
vector<int> inorderTraversal(TreeNode* root) {
vector<int> inorder;
TreeNode *node = root;
while (node)
{
TreeNode *leftRightMost = node->left;
while (leftRightMost && leftRightMost->right)
{
leftRightMost = leftRightMost->right;
}
if (leftRightMost)
{
leftRightMost->right = node;
TreeNode *left = node->left;
node->left = NULL; // to avoid the infinite loop.
node = left;
}
else
{
inorder.push_back(node->val);
node = node->right;
}
}
return inorder;
}
};
class Solution {
public:
vector<int> inorderTraversal(TreeNode* root) {
vector<int> inorder;
TreeNode *curr = root;
while (curr)
{
if (curr->left)
{
TreeNode *leftRightMost = curr->left;
while (leftRightMost->right)
{
leftRightMost = leftRightMost->right;
}
leftRightMost->right = curr;
TreeNode *left = curr->left;
curr->left = NULL; // to avoid the infinite loop.
curr = left;
}
else{
inorder.push_back(curr->val);
curr = curr->right;
}
}
return inorder;
}
};
``````