Guest User

Untitled

a guest
Feb 24th, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.86 KB | None | 0 0
  1. /**
  2. * Definition for a binary tree node.
  3. * public class TreeNode {
  4. * int val;
  5. * TreeNode left;
  6. * TreeNode right;
  7. * TreeNode(int x) { val = x; }
  8. * }
  9. */
  10. class Solution {
  11. public List<Integer> inorderTraversal(TreeNode root) {
  12. List<Integer> res = new ArrayList<>();
  13.  
  14. // Stack store nodes to backtrack to
  15. Stack<TreeNode> stack = new Stack<>();
  16.  
  17. // Point to current node
  18. TreeNode curr = root;
  19.  
  20. while (!stack.isEmpty() || curr != null) {
  21. if (curr != null) {
  22. // Preserve current node and move to left
  23. stack.push(curr);
  24. curr = curr.left;
  25. } else {
  26. curr = stack.pop();
  27. res.add(curr.val);
  28. curr = curr.right;
  29. }
  30. }
  31.  
  32. return res;
  33. }
  34. }
Add Comment
Please, Sign In to add comment