title: How can I get all leaf nodes of a tree?
body: <p>Suppose I have a node in a tree, how can I get all leaf nodes whose ancestor is this node?</p>
  <p>I have defined the TreeNode like this:</p>
  <pre><code>public class TreeNode&lt;T&gt;
  {
      /** all children of the node */
      private List&lt;TreeNode&lt;T&gt;&gt; children = new ArrayList&lt;TreeNode&lt;T&gt;&gt;();
      /** the parent of the node, if the node is root, parent = null */
      private TreeNode&lt;T&gt; parent = null;
      /** the stored data of the node */
      private T data = null;
  
      /** the method I want to implement */
      public Set&lt;TreeNode&lt;T&gt;&gt; getAllLeafNodes()
      {
          Set&lt;TreeNode&lt;T&gt;&gt; leafNodes = new HashSet&lt;TreeNode&lt;T&gt;&gt;();
          return leafNodes;
      }
  }
  </code></pre>