1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| public class BinaryTreeInorderTraversal { public List<Integer> inorderTraversal(TreeNode root) { List<Integer> result = new ArrayList<Integer>(); if (root == null) { return result; } helper(root, result); return result; } private void helper(TreeNode root, List<Integer> result) { if (root != null && root.left == null && root.right == null) { result.add(root.val); } else { if (root.left != null) { helper(root.left, result); } result.add(root.val); if (root.right != null) { helper(root.right, result); } } } }
|