sweet1cris

Untitled

Jan 9th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.91 KB | None | 0 0
  1. /**
  2.  * Definition of TreeNode:
  3.  * public class TreeNode {
  4.  *     public int val;
  5.  *     public TreeNode left, right;
  6.  *     public TreeNode(int val) {
  7.  *         this.val = val;
  8.  *         this.left = this.right = null;
  9.  *     }
  10.  * }
  11.  */
  12. public class Solution {
  13.     /**
  14.      * @param root: The root of binary tree.
  15.      * @return: Inorder in ArrayList which contains node values.
  16.      */
  17.     public ArrayList<Integer> inorderTraversal(TreeNode root) {
  18.         Stack<TreeNode> stack = new Stack<TreeNode>();
  19.         ArrayList<Integer> result = new ArrayList<Integer>();
  20.         TreeNode curt = root;
  21.         while (curt != null || !stack.empty()) {
  22.             while (curt != null) {
  23.                 stack.add(curt);
  24.                 curt = curt.left;
  25.             }
  26.             curt = stack.pop();
  27.             result.add(curt.val);
  28.             curt = curt.right;
  29.         }
  30.         return result;
  31.     }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment