Find child in non-binary tree (recursively)
<p>I have TreeNode class - implementation of the node of the non-binary tree (<code>List&lt;TreeNode&gt; children</code>).</p>

<p>I need find the first node with the given data among the children of this. I wrote some method, but there is some problem obviously (<code>java.lang.AssertionError: Failed to find a child with not-null data: expected:&lt;2&gt; but was:&lt;null&gt;</code>). (if data is null I need to return first child with null data). </p>

<pre><code>public TreeNode findChild(Object data) {


    if (data == null) {
        Iterator&lt;TreeNode&gt; a = getChildrenIterator();
        TreeNode tmp;
        while (a.hasNext()) {
            tmp = a.next();
            if (tmp.getData()==null) return tmp;
            tmp.findChild(data);
        }
    }else
    {
        Iterator&lt;TreeNode&gt; a = getChildrenIterator();
        TreeNode tmp;
        while (a.hasNext()) {
            tmp = a.next();
            if (data.equals(tmp.getData())) return tmp;
            tmp.findChild(data);
        }
    }

    return null;
}
</code></pre>