Created
June 11, 2015 10:05
-
-
Save codetalks-new/802dfd67e078df19cf11 to your computer and use it in GitHub Desktop.
leetcode https://leetcode.com/problems/binary-tree-inorder-traversal/ Java Solution
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 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