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

  • Home
  • SEARCH
  • 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 540439
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T10:14:25+00:00 2026-05-13T10:14:25+00:00

I have a question about the following code private void printTree(Node node){ if(node==null) return;

  • 0

I have a question about the following code

private void printTree(Node node){
    if(node==null) return;
    printTree(node.left);
    System.out.print(node.data+" ");
    printTree(node.right);
}

I don’t really get the point of ‘return;’ statement there. It looks like if node is null, code returns nothing. but then without that line, a compiler generates an exception error.

  • 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-05-13T10:14:25+00:00Added an answer on May 13, 2026 at 10:14 am

    This is a recursive function (one that calls itself repeatedly). The purpose of the return is to ensure that it doesn’t attempt to do so forever, resulting in a null pointer exception as you run off the bottom of the tree.

    What will happen is that the first time you call this function with a node (usually, but not always, the root node), it will first print out the left sub-tree of that node, then the value of the node itself, then the right sub-tree of that node.

    The way it prints out the sub-trees is by calling itself with the top level node of that sub-tree. This is a very common method of elegantly processing recursive structures.

    The test for null is so that it has a condition where the search down through the levels of the tree stops when it reaches a node that has no children on the particular side you’re examining (left or right).

    By way of example, let’s say you have the following tree (uppercase letters with their numbers are real nodes with the numbers being their value, and === markers are nulls):

                 A26                Level 0
                  |
           +------+------+
           |             |
          B14           C84         Level 1
           |             |
        +--+--+       +--+--+
        |     |       |     |
       D11   ===     ===   E99      Level 2
        |                   |
     +--+--+             +--+--+
     |     |             |     |
    ===   ===           ===   ===   Level 3
    

    Here’s what will happen, when you call the function with A.

    You call the function (level 0) with A.
      The function will call itself (level 1) with B (A left).
        The function will call itself (level 2) with D (B left).
          The function will call itself (level 3) with null (D left).
            The function will return to level 2.
          The function will print out 11 from D.
          The function will call itself (level 3) with null (D right).
            The function will return to level 2.
          The function will return to level 1.
        The function will print out 14 from B.
        The function will call itself (level 2) with null (B right).
          The function will return to level 1.
        The function will return to level 0.
      The function will print out 26 from A.
      The function will call itself (level 1) with C (A right).
        The function will call itself (level 2) with null (C left).
          The function will return to level 1.
        The function will print out 84 from C.
        The function will call itself (level 2) with E (C right).
          The function will call itself (level 3) with null (E left).
            The function will return to level 2.
          The function will print out 99 from E.
          The function will call itself (level 3) with null (E right).
            The function will return to level 2.
          The function will return to level 1.
        The function will return to level 0.
      The function will return to you.
    

    The upshot is that it’s printed out the sequence DBACE which, in a sorted tree, is the elements in sorted order (11, 14, 26, 84, 99).


    Or a simpler version if you can’t be bothered to read through my voluminous explanation above:

                 A26                Level 0
                  |
           +------+------+
           |             |
          B14           ===         Level 1
           |
        +--+--+
        |     |
       ===   ===                    Level 2
    
    You call the function (level 0) with A.
      The function will call itself (level 1) with B (A left).
        The function will call itself (level 2) with null (B left).
          The function will return to level 1.
        The function will print out 14 from B.
        The function will call itself (level 2) with null (B right).
          The function will return to level 1.
        The function will return to level 0.
      The function will print out 26 from A.
      The function will call itself (level 1) with null (A right).
        The function will return to level 0.
      The function will return to you.
    

    In that case, you’d get BA or (14,26).

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

Sidebar

Ask A Question

Stats

  • Questions 379k
  • Answers 380k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Is this possible? Yes it is, but the trend is… May 14, 2026 at 9:42 pm
  • Editorial Team
    Editorial Team added an answer It's always good to show a complete working code snippet:… May 14, 2026 at 9:42 pm
  • Editorial Team
    Editorial Team added an answer Use the built in invoice settings to change the taxes.… May 14, 2026 at 9:42 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.