-
-
Save InterviewBytes/1d74444ebe91c90a35ea03f79e5ae825 to your computer and use it in GitHub Desktop.
Inorder Traversal Recursive
This file contains 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
package com.interviewbytes.trees; | |
import java.util.ArrayList; | |
import java.util.List; | |
public class InorderTraversalRecursive { | |
public List<Integer> inorderTraversal(TreeNode root) { | |
List<Integer> list = new ArrayList<>(); | |
helper(list, root); | |
return list; | |
} | |
private void helper(List<Integer> list, TreeNode node) { | |
if (node == null) return; | |
helper(list, node.left); | |
list.add(node.val); | |
helper(list, node.right); | |
} | |
} |
This file contains 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
package com.interviewbytes.trees; | |
public class TreeNode { | |
int val; | |
TreeNode left; | |
TreeNode right; | |
TreeNode(int x) { | |
val = x; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment