DFS Recursion:
public void DFS(TreeNode root){
if(root == null){
return;
}
System.out.println(root.val);
DFS(root.left);
DFS(root.right);
}
DFS Iteration:
public void DFS(TreeNode root){
if(root == null){
return;
}
Stack<TreeNode> stk = new Stack<TreeNode>();
stk.push(root);
while(!stk.empty()){
TreeNode tn = stk.pop();
System.out.println(tn.val);
if(tn.right != null){
stk.push(tn.right);
}
if(tn.left != null){
stk.push(tn.left);
}
}
}