patternjavaMinor
DFS in Binary Tree
Viewed 0 times
dfsbinarytree
Problem
I have written this code for DFS in a binary tree and would like improvements on it.
```
// Method 1: Recursive DFS
public static boolean DFS(Node root, int k){
if(root == null){
return false;
} else if (root.data == k){
return true;
} else {
return DFS(root.left, k) || DFS(root.right, k);
}
}
//=============================================================================
// Method 2: DFS using stack
public static boolean DFS2(Node root, int k){
if(root == null){
return false;
}
Stack stack = new Stack();
stack.push(root);
while(!stack.isEmpty()){
Node current = stack.pop();
if(current.data == k){
return true; //Found the value!
}
if(current.right != null){
stack.push(current.right);
}
if(current.left != null){ // As we want to visit left
stack.push(current.left); //child first, we must push this node last
}
}
return false; // Not found
}
//============================================================================
// Method 3: DFS by marking visited nodes - using stack
public static boolean DFS4(Node root, int k) {
if(root == null){
return false;
}
Stack stack = new Stack();
stack.push(root);
while (!stack.isEmpty()) {
Node current = stack.pop();
if(current.data == k){
return true;
}
current.visited = true;
if (current.right != null && current.right.visited == false) {
stack.push(current.right);
}
if (current.left != null && current.left.visited == false) {
stack.
```
// Method 1: Recursive DFS
public static boolean DFS(Node root, int k){
if(root == null){
return false;
} else if (root.data == k){
return true;
} else {
return DFS(root.left, k) || DFS(root.right, k);
}
}
//=============================================================================
// Method 2: DFS using stack
public static boolean DFS2(Node root, int k){
if(root == null){
return false;
}
Stack stack = new Stack();
stack.push(root);
while(!stack.isEmpty()){
Node current = stack.pop();
if(current.data == k){
return true; //Found the value!
}
if(current.right != null){
stack.push(current.right);
}
if(current.left != null){ // As we want to visit left
stack.push(current.left); //child first, we must push this node last
}
}
return false; // Not found
}
//============================================================================
// Method 3: DFS by marking visited nodes - using stack
public static boolean DFS4(Node root, int k) {
if(root == null){
return false;
}
Stack stack = new Stack();
stack.push(root);
while (!stack.isEmpty()) {
Node current = stack.pop();
if(current.data == k){
return true;
}
current.visited = true;
if (current.right != null && current.right.visited == false) {
stack.push(current.right);
}
if (current.left != null && current.left.visited == false) {
stack.
Solution
I would retain
DFS2 and get rid of all the other implementations. Reason: DFS is cool also, but as it is recursive, you may get a stack overflow on trees with large maximum depth. What comes to DFS4 and DFS5, since you work on trees, there is no need for visited set as trees are acyclic. Also, what comes to coding style, I would love seeing an empty line before and after each if or while statement; I think that adds up to readability.Context
StackExchange Code Review Q#105078, answer score: 4
Revisions (0)
No revisions yet.