Topic: Tree
1) Iterative Preorder Traversal | Easy |
Question
Input:
1
/
4
/ \
4 2
class Node{
int data;
Node left,right;
Node(int d){
data=d;
left=right=null;
}
}
class BinaryTree
{
static ArrayList<Integer> preorder(Node root)
{
ArrayList<Integer> al = new ArrayList<Integer>();
if(root==null){
return al;
}
Stack<Node> st = new Stack<Node>();
st.push(root);
while(!st.isEmpty()){
root = st.pop();
al.add(root.data);
if(root.right!=null){
st.push(root.right);
}
if(root.left!=null){
st.push(root.left);
}
}
return al;
}
}
Output: 1 4 4 2
Thank you for reading:)