title: Find rightmost children in binary tree java
body: <p>I'm having some trouble finding the last element (the rightmost child) in my binary tree. </p>
  
  <p>This is what I have so far:</p>
  
  <pre><code>public Node findLastElement(Node root) {
    Node x = root;
    if (x != null)
        findLastElement(x.right);
    return x;
  }
  </code></pre>
  
  <p>If I print the elements, the last one that prints is the last element, but I can't seem to "get" that element. When i try to return x after the loop, I get a nullpointer. How can I save the last element and return it? </p>