title: Is there a more concise way to find the top node in a tree that meets a criterion
body: <p>For a given node in a tree, I'm trying to find its highest ancestor (or the node itself), but without crossing a certain "barrier" in the tree (a specific node type) - if there is such a "barrier" node. If there is no "barrier" node, I want to return the top node in the tree.</p>
  
  <p>Sounds more complicated than it is - here is some code:</p>
  
  <pre><code>    public Node GetHighestRelevantAncestorOrSelf(Node node)
      {
          Node topNode = node;
          bool newTopNodeFound;
          do
          {
              Node newTopNode = GetParent(topNode);
  
              newTopNodeFound = (newTopNode != null &amp;&amp; !IsBarrierNode(newTopNode));
  
              if (newTopNodeFound)
              {
                  topNode = newTopNode;
              }
          } while (newTopNodeFound);
  
          return topNode;
      }
  </code></pre>
  
  <p>Is there a better way to write this, i.e. without evaluating "newTopNodeFound" twice? Maybe using a for loop?</p>