Skip to content

Instantly share code, notes, and snippets.

@guolinaileen
Last active December 14, 2015 17:29
Show Gist options
  • Save guolinaileen/5122853 to your computer and use it in GitHub Desktop.
Save guolinaileen/5122853 to your computer and use it in GitHub Desktop.
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public ArrayList<Integer> inorderTraversal(TreeNode root) {
// Start typing your Java solution below
// DO NOT write main() function
ArrayList<Integer> result=new ArrayList<Integer>();
if(root==null) return result;
Stack<TreeNode> stack=new Stack<TreeNode>();
while(!stack.isEmpty()|| root!=null)
{
if(root!=null)
{
stack.push(root); root=root.left;
}else
{
root=stack.pop(); result.add(root.val); root=root.right;
}
}
return result;
}
}
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public ArrayList<Integer> inorderTraversal(TreeNode root) {
// Start typing your Java solution below
// DO NOT write main() function
if(root==null) return new ArrayList<Integer>();
ArrayList<Integer> result=inorderTraversal(root.left);
result.add(root.val);
ArrayList<Integer> right=inorderTraversal(root.right);
result.addAll(right);
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment