Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Definition for a binary tree node.
- * public class TreeNode {
- * int val;
- * TreeNode left;
- * TreeNode right;
- * TreeNode(int x) { val = x; }
- * }
- */
- class Solution {
- public List<Integer> inorderTraversal(TreeNode root) {
- List<Integer> res = new ArrayList<>();
- // Stack store nodes to backtrack to
- Stack<TreeNode> stack = new Stack<>();
- // Point to current node
- TreeNode curr = root;
- while (!stack.isEmpty() || curr != null) {
- if (curr != null) {
- // Preserve current node and move to left
- stack.push(curr);
- curr = curr.left;
- } else {
- curr = stack.pop();
- res.add(curr.val);
- curr = curr.right;
- }
- }
- return res;
- }
- }
Add Comment
Please, Sign In to add comment