Trying to count left nodes in a binary tree - binary-tree

I have some cases passing with this code but others are failing.. is it my logic or is it the order of if statements..please help.. using practiceitcs website
public int countLeftNodes() {
return countLeftNodes(overallRoot);
}
private int countLeftNodes(IntTreeNode root){
int count = 0;
if (root== null) return count;
if (root.right == null && root.left == null)
return count;
if(root.right != null)  count += countLeftNodes(root.left);
if (root.left!= null)
count += 1 + countLeftNodes(root.left);
return count;
}

Related

Depth first search on Neo4j with filtering on node properties

I would like to perform a depth first search on my graph and so, get all the paths existing from a given node ('N1456' in my example), and all the nodes of theses path must have the same property "PROPERTY_TO_FILTER".
Typically, my graph is composed of two types of node, and two types of relations.
For now, I tested the following request :
WITH "
MATCH (my_node{name : 'N1456'})
CALL apoc.path.expandConfig(protein, {uniqueness:'NODE_GLOBAL', bfs : FALSE}) YIELD path
WITH path, my_node, last(nodes(path)) as subgraph
WHERE my_node<> subgraph and my_node.my_property CONTAINS 'PROPERTY_TO_FILTER'
RETURN nodes(path), length(path) AS len
ORDER BY len DESC" AS query
CALL apoc.export.json.query(query, "my_results.json", {})
YIELD properties, data
RETURN properties, data;
However, the results are not the ones attended. I get a list of paths but only the first node has the property "PROPERTY_TO_FILTER" ; this filter is not taken into account for the other nodes...
I guess I should put a filter at apoc.path.expandConfig level, but I see in the documentation that this is only possible to filter the node label, not the node properties.
Could someone help please ?
Maybe this can help:
MATCH(fromNode:LABEL{name : 'N1456'})-[r:REL_TO_TRAVERSE*1..2]->(toNode:LABEL)
WHERE toNode.my_property CONTAINS 'PROPERTY_TO_FILTER'
RETURN fromNode,r,toNode
It's called variable length pattern matching:
https://neo4j.com/docs/cypher-manual/current/syntax/patterns/#cypher-pattern-varlength

Xquery - return only distinct attributes from if/then

I've been trying to write a query to get distinct attribute values after using if/then to determine whether I'll use the element in the first place. Here's my example xml and the query i've written so far:
<donors>
<donor donor_id="x21" cn_id="x12">
<homeless>$1201</homeless>
<conservation>$300</conservation>
<cancerResearch>$250</cancerResearch>
</donor>
<donor donor_id="x23" cn_id="x13">
<homeless>$121</homeless>
<conservation>$30</conservation>
<cancerResearch>$50</cancerResearch>
</donor>
<donor donor_id="x24" cn_id="x14">
<homeless>$1201</homeless>
<cancerResearch>$250</cancerResearch>
</donor>
<donor donor_id="x25" cn_id="x12">
<homeless>$1201</homeless>
<conservation>$300</conservation>
<cancerResearch>$250</cancerResearch>
</donor>
</donors>
I want to first get all donors who have a child "conservation". I've done the following for that:
<conservationists>
{
for $x in //donor
return
if(exists($x/conservation))
then <conservationist cn_id="{$x/#cn_id}/>
else ()
}
</conservationists>
I tried wrapping the whole thing in distinct-values but that just gave nothing, and every where else I tried doing something to that effect I just ended up with an end tag.
This is one possible way :
<conservationists>
{
for $x in distinct-values(//donor[conservation]/#cn_id)
return
<conservationist cn_id="{$x}"/>
}
</conservationists>
xpathtester demo
The expression distinct-values(//donor[conservation]/#cn_id) returns distinct values of cn_id attribute from donor elements that have at least one conservation child element.

Selecting several elements from a grid using while loop and xpath in Selenium webDriver

I have a while loop containing an Xpath expression. How can I have the while loop working properly with only the value of tr[index] changing within the Xpath? Below is an example:
//this is what I currently have:
while(webDriver.findElement(By.xpath("//*[#id='sharing_list']/tbody/tr[1]/td[4]")).isDisplayed())
{
webDriver.findElement(By.xpath("//*[#id='sharing_list']/tbody/tr[1]/td[4]/span")).click();
}
//and this is what I would like to have:
int n=1;
while(webDriver.findElement(By.xpath("//*[#id='sharing_list']/tbody/tr[n]/td[4]")).isDisplayed())
{
webDriver.findElement(By.xpath("//*[#id='sharing_list']/tbody/tr[n]/td[4]/span")).click();
n++;
}
Does any one have an idea how to make it work? I am using Selenium 2.33.
Your help will be very appreciated.
This is really a String issue. Replace your tr[n] with tr["+n+"]

How to get H1,H2,H3,... using a single xpath expression

How can I get H1,H2,H3 contents in one single xpath expression?
I know I could do this.
//html/body/h1/text()
//html/body/h2/text()
//html/body/h3/text()
and so on.
Use:
/html/body/*[self::h1 or self::h2 or self::h3]/text()
The following expression is incorrect:
//html/body/*[local-name() = "h1"
or local-name() = "h2"
or local-name() = "h3"]/text()
because it may select text nodes that are children of unwanted:h1, different:h2, someWeirdNamespace:h3.
Another recommendation: Always avoid using // when the structure of the XML document is statically known. Using // most often results in significant inefficiencies because it causes the complete document (sub)tree roted in the context node to be traversed.

JDOM-XPath: Can't get the second value in a collection

I'm pretty confused about this one. Given the following xml:
<sch:eventList>
<sch:event>
<sch:eventName>Event One</sch:eventName>
<sch:locationName>Location One</sch:locationName>
</sch:event>
<sch:event>
<sch:eventName>Event Two</sch:eventName>
<sch:locationName>Location Two</sch:locationName>
</sch:event>
</sch:eventList>
When using JDOM using the following code:
XPath eventNameExpression = XPath.newInstance("//sch:eventName");
XPath eventLocationExpression = XPath.newInstance("//sch:eventLocation");
XPath eventExpression = XPath.newInstance("//sch:event");
List<Element> elements = eventExpression.selectNodes(requestElement);
for(Element e: elements) {
System.out.println(eventNameExpression.valueOf(e));
System.out.println(eventLocationExpression.valueOf(e));
}
The console shows this:
Event One
Location One
Event One
Location One
What am I missing?
Don't use '//' it starts always searching at the root node. Use e.g. './sch:eventName' it is relative to the current node.

Resources