Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong tree traversal sequence #210

Open
kaleem68 opened this issue Nov 13, 2020 · 0 comments
Open

Wrong tree traversal sequence #210

kaleem68 opened this issue Nov 13, 2020 · 0 comments

Comments

@kaleem68
Copy link

kaleem68 commented Nov 13, 2020

Java/Ch 04. Trees and Graphs/Introduction/Traversals.java Please fix the recursive call for the preorder and postorder function.

Here is what is found inside Traversals.java

public static void preOrderTraversal(TreeNode node) {
    if (node != null) {
        visit(node);
	inOrderTraversal(node.left);
	inOrderTraversal(node.right);
    }
}

public static void postOrderTraversal(TreeNode node) {
    if (node != null) {
	inOrderTraversal(node.left);
	inOrderTraversal(node.right);
        visit(node);
    }

}

Make it as following
public static void preOrderTraversal(TreeNode node) {
if (node != null) {
visit(node);
preOrderTraversal(node.left);
preOrderTraversal(node.right);
}
}

public static void postOrderTraversal(TreeNode node) {
    if (node != null) {
    postOrderTraversal(node.left);
    postOrderTraversal(node.right);
        visit(node);
    }

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant