title: similarTrees in java
body: <p>This is what I have to do. The Binary Search Tree ADT is extended to include a bool ean method si m i l ar Trees that receives references to two binary trees and determines whether the shapes of the trees are the same. (The nodes do not have to contain the same values, but each node must have the same number of children.) I have code to show what I have. </p>
  
  <pre><code>public static &lt;T&gt; boolean  similarTrees(BSTNode&lt;T&gt; a, BSTNode&lt;T&gt; b) {
      // check for reference equality and nulls
      if (a == b) return true; // note this picks up case of two nulls
      if (a == null) return false;
      if (b == null) return false;
  
      // check for data inequality
      if (a.data != b.data) {
          if ((a.data == null) || (b.data == null)) return false;
          if (!(a.data.equals(b.data))) return false;
      }
  
      // recursively check branches
      if (!similarTrees(a.left, b.left)) return false;
      if (!similarTrees(a.right, b.right)) return false;
  
      // we've eliminated all possibilities for non-equality, so trees must be equal
      return true;
  }
  </code></pre>