Last active
December 14, 2015 17:29
-
-
Save guolinaileen/5122853 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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