-
-
Notifications
You must be signed in to change notification settings - Fork 442
Expand file tree
/
Copy pathbinary-tree-inorder-traversal.java
More file actions
37 lines (32 loc) · 880 Bytes
/
binary-tree-inorder-traversal.java
File metadata and controls
37 lines (32 loc) · 880 Bytes
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
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*
* Runtime: 0 ms
* Memory Usage: 37.8 MB
*
*/
class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> list = new ArrayList<Integer>();
inorder(root, list);
return list;
}
public void inorder(TreeNode root, List<Integer> list){
/* Inorder traversal is visiting : left subtree then root then right subtree */
/* if node is null, then return back to the caller function */
if(root==null)
return;
/* Recurse for left subtree */
inorder(root.left, list);
/* Add the current node's value to the list */
list.add(root.val);
/* Recurse for right subtree */
inorder(root.right, list);
}
}