public static List<Integer> inorderTraversal(TreeNode root){
  List<Integer> result=new ArrayList<Integer>();
  Stack<TreeNode> s=new Stack<TreeNode>();
  while (!s.isEmpty() || root != null) {
    if (root != null) {
      s.push(root);
      root=root.left;
    }
 else {
      root=s.pop();
      result.add(root.val);
      root=root.right;
    }
  }
  return result;
}

public static List < Integer > inorderTraversal ( TreeNode root ) { List < Integer > result = new ArrayList < Integer > ( ) ; Stack < TreeNode > s = new Stack < TreeNode > ( ) ; while ( ! s . isEmpty ( ) || root != null ) { if ( root != null ) { s . push ( root ) ; root = root . left ; } else { root = s . pop ( ) ; result . add ( root . val ) ; root = root . right ; } } return result ; }
stack solution , o ( n ) space use a stack to store treenodes go to left most and add each node pop the node from stack , add its value , and try to go right stop if stack is empty or node is null
