Example: 1 / \ 2 3 ==> 4251637 / \ / \ 4 5 6 7
left
subtree by recursively calling the Inorder method. data
part of root element(or current element) in the arraylist right
subtree by recursively calling the inorder method.
add
method to add the node's data in the inorderedList list
1. Recurse through the left nodes
starting from the root. Data of the elements in the left subtree will get added to the ArrayList.
2. Add the data of the root node to the ArrayList
.
3. Recurse through the right nodes
. Data of the elements in the right subtree will get added to the ArrayListt.
//Populate the data in the below list in inorder. public ArrayList<Integer> inorderedList = new ArrayList<Integer>(); public void inorder(TreeNode root) { }
C
Java
Python