
Given a binary tree, return the inorder traversal of its nodes’ values.
Example:
1 2 3 4 5 6 7 8
|
Input: [1,null,2,3] 1 2 / 3
Output: [1,3,2]
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
|
class { public List<Integer> inorderTraversal(TreeNode root) { List<Integer> list = new ArrayList<>(); Deque<TreeNode> stack = new ArrayDeque<>(); TreeNode curr = root; while (curr != null || ! stack.isEmpty()) { if (curr != null) { stack.push(curr); curr = curr.left; } else { curr = stack.pop(); list.add(curr.val); curr = curr.right; } } return list; } }
|
近期评论