Given a binary tree, return the inorder traversal of its nodes' values.
For example:
Given binary tree {1,#,2,3}
,
1 \ 2 / 3
return [1,3,2]
.
Note: Recursive solution is trivial, could you do it iteratively?
confused what "{1,#,2,3}"
means? > read more on how binary tree is serialized on OJ.
二叉树的中序遍历顺序为左-根-右,可以有递归和非递归来解,非递归解法又分为两种,一种是使用栈来接,另一种不需要使用栈的Morris方法。
Morris方法可参考帖子:Morris Traversal方法遍历二叉树(非递归,不用栈,O(1)空间)
Java: Without Recursion, using Stack
public class Solution {
/**
* @param root: The root of binary tree.
* @return: Inorder in ArrayList which contains node values.
*/
public ArrayList<Integer> inorderTraversal(TreeNode root) {
Stack<TreeNode> stack = new Stack<TreeNode>();
ArrayList<Integer> result = new ArrayList<Integer>();
TreeNode curt = root;
while (curt != null || !stack.empty()) {
while (curt != null) {
stack.add(curt);
curt = curt.left;
}
curt = stack.pop();
result.add(curt.val);
curt = curt.right;
}
return result;
}
}
Python: Recursion, Time: O(n), Space: O(h) # h is the height of tree.
class Solution(object):
def inorderTraversal(self, root):
res = []
if root:
res = self.inorderTraversal(root.left)
res.append(root.val)
res = res + self.inorderTraversal(root.right)
return res
Python: Recursion, briefly done
class Solution(object):
def inorderTraversal(self, root):
return (self.inorderTraversal(root.left) + [root.val] + self.inorderTraversal(root.right)) if root else []
Pyhton: Without recursion, using stack, Time: O(n), Space: O(h) # h is the height of tree.
class Solution2(object):
def inorderTraversal(self, root):
"""
:type root: TreeNode
:rtype: List[int]
"""
result, stack = [], [(root, False)]
while stack:
root, is_visited = stack.pop()
if root is None:
continue
if is_visited:
result.append(root.val)
else:
stack.append((root.right, False))
stack.append((root, True))
stack.append((root.left, False))
return result
Python: Morris Inorder Traversal, Without recursion, Without stack, Time: O(n), Space: O(1)
class Solution(object):
def inorderTraversal(self, root):
"""
:type root: TreeNode
:rtype: List[int]
"""
result, curr = [], root
while curr:
if curr.left is None:
result.append(curr.val)
curr = curr.right
else:
node = curr.left
while node.right and node.right != curr:
node = node.right
if node.right is None:
node.right = curr
curr = curr.left
else:
result.append(curr.val)
node.right = None
curr = curr.right
return result
C++: Recursion
class Solution {
/**
* @param root: The root of binary tree.
* @return: Inorder in vector which contains node values.
*/
public:
vector<int> inorder;
void traverse(TreeNode *root) {
if (root == NULL) {
return;
}
traverse(root->left);
inorder.push_back(root->val);
traverse(root->right);
}
vector<int> inorderTraversal(TreeNode *root) {
inorder.clear();
traverse(root);
return inorder;
}
};
C++: Using Stack.
class Solution2 {
public:
vector<int> inorderTraversal(TreeNode* root) {
vector<int> res;
stack<pair<TreeNode *, bool>> s;
s.emplace(root, false);
while (!s.empty()) {
bool visited;
tie(root, visited) = s.top();
s.pop();
if (root == nullptr) {
continue;
}
if (visited) {
res.emplace_back(root->val);
} else {
s.emplace(root->right, false);
s.emplace(root, true);
s.emplace(root->left, false);
}
}
return res;
}
};
C++: Morris
class Solution {
public:
vector<int> inorderTraversal(TreeNode* root) {
vector<int> res;
TreeNode *curr = root;
while (curr) {
if (!curr->left) {
res.emplace_back(curr->val);
curr = curr->right;
} else {
TreeNode *node = curr->left;
while (node->right && node->right != curr) {
node = node->right;
}
if (!node->right) {
node->right = curr;
curr = curr->left;
} else {
res.emplace_back(curr->val);
node->right = nullptr;
curr = curr->right;
}
}
}
return res;
}
};
类似题目:
[LeetCode] 144. Binary Tree Preorder Traversal 二叉树的先序遍历
[LeetCode] 145. Binary Tree Postorder Traversal 二叉树的后序遍历