Java not returning correct result for tree search when incrimenting versus adding one

how to handle duplicates in binary search tree java
remove duplicates in binary search tree
binary tree
construct binary search tree
binary search tree insertion
inorder traversal
java program to implement binary search tree
binary search tree rules

Hopefully this will be an easy question. I am trying to find the level of a given unique number on a binary tree. My binary tree is such that

[1,2,3,null,4,null,5]

I am trying to check if the heights of two nodes are the same, in this case, 4 and 5. I implemented a solution as follows.

public int findNodeHeight(TreeNode root, int nodeValue, int height){
    //if end of tree, exit loop
    if(root == null){
        return -1;
    }
    //if we found the value, return height
    if(root.val == nodeValue){
        return height;
    }

    //check the left
    int left = findNodeHeight(root.left,nodeValue,height++);
    if(left!=-1){
        return left;
    }
    //check the right
    int right = findNodeHeight(root.right,nodeValue,height++);
    if(right!=-1){
        return right;
    }

    return -1;
}

However this doesn't work, and it returns a height of 2 for 5 (which is correct) and a height of 1 for 4 (which is incorrect). However if I change the ++ operator to

height+1 

as follows:

public int findNodeHeight(TreeNode root, int nodeValue, int height){

    if(root == null){
        return -1;
    }

    if(root.val == nodeValue){
        return height;
    }

    //check the left
    int left = findNodeHeight(root.left,nodeValue,height+1);
    if(left!=-1){
        return left;
    }
    //check the right
    int right = findNodeHeight(root.right,nodeValue,height+1);
    if(right!=-1){
        return right;
    }

    return -1;
}

The solution now works. Why is it wrong to use the ++ operator in the method call?

Thanks!

The reason is that the method is executed with height value when you pass height++ and then the value of height is incremented by 1.

height++ is a post-increment operator, which would increment the value of height by 1 only after the execution of the statement. ++height is a pre-increment operator, which would increment the value of height by 1 before the execution of the statement.

If you use the pre-increment operator on height, it might work fine in cases where the second findNodeHeight call is not made at all, but might result in anomalies when the second findNodeHeight call is made since it would have updated the value of height on the second call. Hence it would be advisable to use height+1 in case it provides the right result, since the increment operators might not provide the right result always.

How to handle duplicates in Binary Search Tree?, In a Binary Search Tree (BST), all keys in left subtree of a key must be smaller and tree node to store count together with regular fields like key, left and right 1) Height of tree is small irrespective of number of duplicates. C++; C; Java; Python3; C# increment count and return Note that the entire tree does not need. amazon-web-services couchbase github java reactjs redux spring spring-boot oop rest agile. This user is part of the top 5% Stack Overflow answerers in these

You cannot use ++ in the method call in this situation because, var++ increments var, but it returns the original var, which means you are simply passing var. Instead if you used ++var, it would both increment var, and pass the newly incremented value.

Count full nodes in a Binary tree (Iterative and Recursive , If the current node is full, we increment result by 1 and add returned values of left and right subtrees. C++; Java; Python3; C#. C++. 2 Java not returning correct result for tree search when incrimenting versus adding one Feb 18 '19 2 Godot TouchScreen swipe detector Jan 11 1 Proper way to get groups of a user in linux using C Sep 11 '19

This is caused because

height++

returns height and height and increments height by one and

height+1

just returns height+1 and it doesn't increments the height which would also be a problem.

Data Structures 101: Binary Search Tree, These 'other' nodes are child nodes, called a left node and right node. Similar to binary search (with an array that is sorted), we cut the amount of As we add to the data structure, we create a new chunk in memory and link to it. no children if(!node.left && !node.right) return null; // one child and it's the� Madhu Bhat's Developer Story. Working as a Software Engineer with the Customer Fulfilment team of Tesco responsible for designing and developing software products which enable fulfilment of online orders of Tesco globally.

Algorithms and Data Structures, Takes two numbers and returns the result (a number); There is a natural precedence among Exercise 1: Draw an expression tree for the expression 10/ 4 + 2*(5+1). If so, we'll store the number here. char op; // If not, we need to know which operator. isLeaf) { return node.value; } // Otherwise do left and right, and add. int� JSON is a text format that is completely language independent but uses conventions that are familiar to programmers of the C-family of languages, including C, C++, C#, Java, JavaScript, Perl, Python, and many others. These properties make JSON an ideal data-interchange language. JSON is built on two structures: A collection of name/value pairs.

Java Recursion with examples, return; } }. This recursion is not infinite, assuming the method is passed a positive which you will solve by returning the result of a second calling of your method. ( recursion ) will pass on the complex problem but reduced by one increment. as searching a maze for a path to an exit or tree based operations (which we will� In your /etc/rc.conf file, make sure syslog is enabled and add a second line with some options: [10-Sep-2009 09:45:37] <Apocalipse> zensyslog run -v10 [10-Sep-2009 09:45:42] <rmatte> syslogd_enable="YES" [10-Sep-2009 09:45:42] <rmatte> syslogd_flags="-ss" [10-Sep-2009 09:45:49] <rmatte> Those two sses (make sure you have two, not just one

[PDF] Recursion Problem 1: Recursion Problem 2: Binary Trees:, This function takes a string as it's argument and returns the same string A null pointer represents a binary tree with no elements-- the empty tree. The formal or is made of a single node, where the left and right pointers On average, a binary search tree algorithm can locate a node in an N Binary Trees in Java: In Java�

Comments
  • What is the value of height when it is initially 2 and you call height++ two times? And what is this value when you call height+1 two times? I guess then you'll understand.
  • Why do you think ++height would work better? Why do you think it is correct pass different height values to the two recursive findNodeHeight calls?
  • i don't think it would work better it's just an alternative that works, actually height + 1 is better for this since it's useless to store the height increment .
  • Well, that's the thing, I don't think it would work. Using ++height two times is something different than using height+1 two times. Thus OP would pass a different height value on the second findNodeHeight call
  • thanks! you are right, i edited the answer so it's no longer wrong.