Skip to content

Instantly share code, notes, and snippets.

@cixuuz
Created August 22, 2017 21:59
Show Gist options
  • Select an option

  • Save cixuuz/3927083fc3ec44bcb0c70c07ada06b09 to your computer and use it in GitHub Desktop.

Select an option

Save cixuuz/3927083fc3ec44bcb0c70c07ada06b09 to your computer and use it in GitHub Desktop.
[94. Binary Tree Inorder Traversal] #leetcode
class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> result = new ArrayList<Integer>();
Stack<TreeNode> stack = new Stack<TreeNode>();
while ( !stack.isEmpty() || root != null ) {
if ( root != null ) {
stack.push(root);
root = root.left;
} else {
TreeNode node = stack.pop();
result.add(node.val);
root = node.right;
}
}
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment