-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryTreeInorderTraversal.java
More file actions
48 lines (38 loc) · 1.11 KB
/
BinaryTreeInorderTraversal.java
File metadata and controls
48 lines (38 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package Leetcode;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
public class BinaryTreeInorderTraversal
{
public List<Integer> inorderTraversal( TreeNode root )
{
List<Integer> result = new ArrayList();
if( root == null )
return result;
Stack<TreeNode> stack = new Stack();
stack.push( root );
while( !stack.isEmpty() )
{
root = root.left;
if( root != null )
stack.push( root );
else
{
root = stack.pop();
result.add( root.val );
if( root.right != null )
stack.push( root.right );
root = root.right;
}
}
return result;
}
public static void main( String[] args )
{
BinaryTreeInorderTraversal obj = new BinaryTreeInorderTraversal();
TreeNode root = new TreeNode(1);
root.right = new TreeNode(2);
root.right.left = new TreeNode(3);
System.out.println( obj.inorderTraversal( root ) );
}
}