Iterative
right child doesn't exist: save all the children on the left chain
does exist: save the top of the stack in the result
Python:
def inorderTraversal(self, root):
res, stack = [], []
while (root or stack) :
while root:
stack.append(root)
root = root.left
curr = stack.pop()
res.append(curr.val)
root = curr.right
return res
C++:
class Solution {
public:
vector<int> inorderTraversal(TreeNode* root) {
vector<int> nodes;
stack<TreeNode*> todo;
while (root || !todo.empty()) {
while (root) {
todo.push(root);
root = root -> left;
}
root = todo.top();
todo.pop();
nodes.push_back(root -> val);
root = root -> right;
}
return nodes;
}
};
// https://leetcode.com/problems/binary-tree-inorder-traversal/discuss/31231/C%2B%2B-Iterative-Recursive-and-Morris
Java:
public class Solution {
public List < Integer > inorderTraversal(TreeNode root) {
List < Integer > res = new ArrayList < > ();
Stack < TreeNode > stack = new Stack < > ();
TreeNode curr = root;
while (curr != null || !stack.isEmpty()) {
while (curr != null) {
stack.push(curr);
curr = curr.left;
}
curr = stack.pop();
res.add(curr.val);
curr = curr.right;
}
return res;
}
}
// https://leetcode.com/problems/binary-tree-inorder-traversal/solution/
Recursive
Python:
def inorderTraversal1(self, root):
"""
:type root: TreeNode
:rtype: List[int]
"""
ans = []
self.helper(root, ans)
return ans
def helper(self, root, ans):
if root:
if root.left:
self.helper(root.left, ans)
ans.append(root.val)
if root.right:
self.helper(root.right, ans)
C++:
class Solution {
public:
vector<int> inorderTraversal(TreeNode* root) {
vector<int> nodes;
inorder(root, nodes);
return nodes;
}
private:
void inorder(TreeNode* root, vector<int>& nodes) {
if (!root) {
return;
}
inorder(root -> left, nodes);
nodes.push_back(root -> val);
inorder(root -> right, nodes);
}
};
// https://leetcode.com/problems/binary-tree-inorder-traversal/discuss/31231/C%2B%2B-Iterative-Recursive-and-Morris
Java:
class Solution {
public List < Integer > inorderTraversal(TreeNode root) {
List < Integer > res = new ArrayList < > ();
helper(root, res);
return res;
}
public void helper(TreeNode root, List < Integer > res) {
if (root != null) {
if (root.left != null) {
helper(root.left, res);
}
res.add(root.val);
if (root.right != null) {
helper(root.right, res);
}
}
}
}
// https://leetcode.com/problems/binary-tree-inorder-traversal/solution/
Mirrors
if the left child exists, move the father and its right children to the right of the right most child of this left child
C++:
class Solution {
public:
vector<int> inorderTraversal(TreeNode* root) {
vector<int> nodes;
while (root) {
if (root -> left) {
TreeNode* pre = root -> left;
while (pre -> right && pre -> right != root) {
pre = pre -> right;
}
if (!pre -> right) {
pre -> right = root;
root = root -> left;
} else {
pre -> right = NULL;
nodes.push_back(root -> val);
root = root -> right;
}
} else {
nodes.push_back(root -> val);
root = root -> right;
}
}
return nodes;
}
};
// https://leetcode.com/problems/binary-tree-inorder-traversal/discuss/31231/C%2B%2B-Iterative-Recursive-and-Morris
Java:
class Solution {
public List < Integer > inorderTraversal(TreeNode root) {
List < Integer > res = new ArrayList < > ();
TreeNode curr = root;
TreeNode pre;
while (curr != null) {
if (curr.left == null) {
res.add(curr.val);
curr = curr.right; // move to next right node
} else { // has a left subtree
pre = curr.left;
while (pre.right != null) { // find rightmost
pre = pre.right;
}
pre.right = curr; // put cur after the pre node
TreeNode temp = curr; // store cur node
curr = curr.left; // move cur to the top of the new tree
temp.left = null; // original cur left be null, avoid infinite loops
}
}
return res;
}
}
// https://leetcode.com/problems/binary-tree-inorder-traversal/solution/