Skip to content

Instantly share code, notes, and snippets.

@codetalks-new
Created June 11, 2015 10:05
Show Gist options
  • Save codetalks-new/802dfd67e078df19cf11 to your computer and use it in GitHub Desktop.
Save codetalks-new/802dfd67e078df19cf11 to your computer and use it in GitHub Desktop.
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> arr = new ArrayList<>();
if(root == null){
return arr;
}
TreeNode current = root;
List<TreeNode> nodeList = new ArrayList<>();
nodeList.add(current);
while(!nodeList.isEmpty()){
while(current.left != null){
nodeList.add(current.left);
current = current.left;
}
TreeNode left = nodeList.remove(nodeList.size() -1 );
arr.add(left.val);
TreeNode right = left.right;
if(right != null){
current = right;
nodeList.add(right);
}
}
return arr;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment