94. Binary Tree Inorder Traversal
# Medium, DFS
Solution 1: Recursive
// Some code
class Solution {
ArrayList<Integer> res = new ArrayList<Integer>();
public List<Integer> inorderTraversal(TreeNode root) {
if(root == null)
return res;
inorderTraversal(root.left);
res.add(root.val);
inorderTraversal(root.right);
return res;
}
}class Solution {
private void inorder(TreeNode root, ArrayList<Integer> res) {
// stop condition
if(root == null) return;
inorder(root.left, res);
res.add(root.val);
inorder(root.right, res);
}
public ArrayList<Integer> inorderTraversal(TreeNode root) {
ArrayList<Integer> res = new ArrayList<Integer>();
inorder(root, res);
return res;
}
}Solution 2: Divide & Conquer
Solution 3: Iterative (while-loop)
Last updated