Skip to content

Commit b4f2f65

Browse files
authored
famous tree questions
1 parent 456d7a1 commit b4f2f65

1 file changed

Lines changed: 21 additions & 0 deletions

File tree

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public List<Integer> inorderTraversal(TreeNode root) {
3+
List<Integer> res = new ArrayList<>();
4+
if (root == null) return res;
5+
Deque<TreeNode> stack = new ArrayDeque<>();
6+
pushLeft(root, stack);
7+
while (!stack.isEmpty()) {
8+
TreeNode cur = stack.pop();
9+
res.add(cur.val);
10+
pushLeft(cur.right, stack);
11+
}
12+
return res;
13+
}
14+
15+
private void pushLeft(TreeNode root, Deque<TreeNode> stack) {
16+
while (root != null) {
17+
stack.push(root);
18+
root = root.left;
19+
}
20+
}
21+
}

0 commit comments

Comments
 (0)