Leetcode 94: Binary Tree Inorder Traversal

Question: https://leetcode.com/problems/binary-tree-inorder-traversal/

Solution:

class Solution {
    List<Integer> result = new ArrayList<>();
    public List<Integer> inorderTraversal(TreeNode root) {
        traverse(root);
        return result;
    }

    private void traverse(TreeNode root) {
        if (root != null) {
            traverse(root.left);
            result.add(root.val);
            traverse(root.right);
        }
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *

Press ESC to close