File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
Given a binary tree, return the inorder traversal of its nodes’ values.
3+
4+
Example :
5+
Given binary tree
6+
7+
1
8+
\
9+
2
10+
/
11+
3
12+
return [1,3,2].
13+
14+
*/
15+
16+
/**
17+
* Definition for binary tree
18+
* class TreeNode {
19+
* int val;
20+
* TreeNode left;
21+
* TreeNode right;
22+
* TreeNode(int x) {
23+
* val = x;
24+
* left=null;
25+
* right=null;
26+
* }
27+
* }
28+
*/
29+
public class Solution {
30+
public ArrayList<Integer> inorderTraversal(TreeNode root) {
31+
ArrayList<Integer> result = new ArrayList<Integer>();
32+
if(root == null)
33+
return result;
34+
Stack<TreeNode> stk = new Stack<TreeNode>();
35+
36+
while(root != null || !stk.isEmpty() )
37+
{
38+
while(root != null)
39+
{
40+
stk.push(root);
41+
root = root.left;
42+
}
43+
root = stk.pop();
44+
result.add(root.val);
45+
root = root.right;
46+
}
47+
return result;
48+
}
49+
}

0 commit comments

Comments
 (0)