Given the root
of a binary tree, return the inorder traversal of its nodes' values.
Example 1:
Input: root = [1,null,2,3] Output: [1,3,2]
Example 2:
Input: root = [] Output: []
Example 3:
Input: root = [1] Output: [1]
Example 4:
Input: root = [1,2] Output: [2,1]
Example 5:
Input: root = [1,null,2] Output: [1,2]
Constraints:
- The number of nodes in the tree is in the range
[0, 100]
. -100 <= Node.val <= 100
Follow up:
Recursive solution is trivial, could you do it iteratively?
1. Recusive Traversal
2. Non-recursive using Stack
3. Morris Traversal
Recusive:
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def inorderTraversal(self, root: TreeNode) -> List[int]:
res = []
def inorder(root):
if root:
inorder(root.left)
res.append(root.val)
inorder(root.right)
inorder(root)
return res
Non-recursive:
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def inorderTraversal(self, root: TreeNode) -> List[int]:
res, s = [], []
while root or s:
if root:
s.append(root)
root = root.left
else:
root = s.pop()
res.append(root.val)
root = root.right
return res
Morris Traversal:
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def inorderTraversal(self, root: TreeNode) -> List[int]:
res = []
while root:
if root.left is None:
res.append(root.val)
root = root.right
else:
pre = root.left
while pre.right and pre.right != root:
pre = pre.right
if pre.right is None:
pre.right = root
root = root.left
else:
res.append(root.val)
pre.right = None
root = root.right
return res
Recursive:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> res = new ArrayList<>();
inorder(root, res);
return res;
}
private void inorder(TreeNode root, List<Integer> res) {
if (root != null) {
inorder(root.left, res);
res.add(root.val);
inorder(root.right, res);
}
}
}
Non-recursive:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> res = new ArrayList<>();
Deque<TreeNode> s = new LinkedList<>();
while (root != null || !s.isEmpty()) {
if (root != null) {
s.offerLast(root);
root = root.left;
} else {
root = s.pollLast();
res.add(root.val);
root = root.right;
}
}
return res;
}
}
Morris Traversal:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> res = new ArrayList<>();
while (root != null) {
if (root.left == null) {
res.add(root.val);
root = root.right;
} else {
TreeNode pre = root.left;
while (pre.right != null && pre.right != root) {
pre = pre.right;
}
if (pre.right == null) {
pre.right = root;
root = root.left;
} else {
res.add(root.val);
pre.right = null;
root = root.right;
}
}
}
return res;
}
}
Recursive:
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @return {number[]}
*/
var inorderTraversal = function(root) {
let res = []
function inorder(root) {
if (root) {
inorder(root.left);
res.push(root.val);
inorder(root.right);
}
}
inorder(root);
return res;
};
Non-recursive:
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @return {number[]}
*/
var inorderTraversal = function(root) {
let res = [];
let s = [];
while (root || s.length > 0) {
if (root) {
s.push(root);
root = root.left;
} else {
root = s.pop();
res.push(root.val);
root = root.right;
}
}
return res;
};
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
vector<int> inorderTraversal(TreeNode* root) {
vector<int> res;
while (root)
{
if (root->left == nullptr)
{
res.push_back(root->val);
root = root->right;
} else {
TreeNode* pre = root->left;
while (pre->right && pre->right != root)
{
pre = pre->right;
}
if (pre->right == nullptr)
{
pre->right = root;
root = root->left;
}
else
{
res.push_back(root->val);
pre->right = nullptr;
root = root->right;
}
}
}
return res;
}
};
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func inorderTraversal(root *TreeNode) []int {
var res []int
for root != nil {
if root.Left == nil {
res = append(res, root.Val)
root = root.Right
} else {
pre := root.Left
for pre.Right != nil && pre.Right != root {
pre = pre.Right
}
if pre.Right == nil {
pre.Right = root
root = root.Left
} else {
res = append(res, root.Val)
pre.Right = nil
root = root.Right
}
}
}
return res
}