Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8599059
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T01:18:26+00:00 2026-06-12T01:18:26+00:00

I have trying print all paths from in a binary tree. I am able

  • 0

I have trying print all paths from in a binary tree. I am able to print all paths in the tree. but the problem is its not printing some redundant zeros in the output. Does arrays get initialized with zeros ? if yes then how to overcome the problem in the case like this ? I am learning and thats why I have posted this question here to learn more and overcome errors.

public class PrintAllPath {

static void printAllPaths(Tree tree) {
    int[] paths = new int[1000];
    printPathsRecur(tree, paths, 0);
}

static void printPathsRecur(Tree tree, int paths[], int pathlen) {
    if (tree == null)
        return;
    paths[pathlen++] = tree.val;
    pathlen++;

    if (tree.left == null && tree.right == null) {
        printArray(paths, pathlen);
    } else {
        printPathsRecur(tree.left, paths, pathlen);
        printPathsRecur(tree.right, paths, pathlen);
    }
}

static void printArray(int paths[], int pathlen) {
    for (int i = 0; i < pathlen; i++) {
        System.out.println("Node : " + paths[i]);
    }
}

static Tree insert(Tree tree, int element) {
    Tree tree2 = new Tree(element, null, null);
    if (tree == null)
        tree = tree2;
    else if (element < tree.val)
        tree.left = insert(tree.left, element);
    else
        tree.right = insert(tree.right, element);
    return tree;
}

static void print(Tree tree) {
    if (tree == null)
        return;
    System.out.println(tree.val);
    print(tree.left);
    print(tree.right);
}

public static void main(String[] args) {
    Tree tree = new Tree(new Integer(5), null, null);
    tree = insert(tree, 10);
    tree = insert(tree, 100);
    tree = insert(tree, 11);
    tree = insert(tree, 110);
    tree = insert(tree, 50);
    System.out.println("The tree is as follows : ");
    print(tree);
    printAllPaths(tree);
}
}

Ouput :

The tree is as follows : 
5
10
100
11
50
110
5 0 10 0 100 0 11 0 50 0 5 0 10 0 100 0 110 0 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T01:18:27+00:00Added an answer on June 12, 2026 at 1:18 am

    Yes, in Java, new int[X] creates a zero-initialized array.

    From JLS §4.12.5:

    Each class variable, instance variable, or array component is initialized with a default value when it is created (§15.9, §15.10) … For type int, the default value is zero, that is, 0.

    Anyway, the reason you’re getting zeros is because you increment pathlen twice every time you insert an element, so you insert an element and then skip the next element (leaving it at 0):

    paths[pathlen++] = tree.val;
    pathlen++;
    

    You only have to increment once.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have names of all files in output.txt but I do not have folderpath
I need some help with my problem. I have created a list of all
I have 145064642 chars long HTML String which I am trying to print. When
I have a simple question concerning Javascript. I am trying to print in a
I'm trying to have Joda print 0 seconds if the input time is zero,
I am trying to have an onclick button print a check and run my
I'm trying to get 4x6 cards to print in Cocoa (story cards). I have
I have trying to learn MVC4 Web API. I am running my project from
I'm kinda new to Flex. I have trying to send Hash from Ruby on
I have been trying to setup git for our web development team unsuccessfully. Some

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.