34,35
public class FindPath {
public void findPath(BinaryTreeNode pRoot,int exceptedSum){
if(pRoot == null){
return ;
}
int currentSum = 0;
List<Integer> list = new ArrayList<>();
findPath(pRoot,exceptedSum,list,currentSum);
}
public void findPath(BinaryTreeNode pRoot, int exceptedSum, List<Integer> list, int currentSum ){
currentSum += pRoot.value;
list.add(pRoot.value);
boolean isLeaf = pRoot.leftNode == null && pRoot.rightNode == null;
if(currentSum == exceptedSum && isLeaf){
for(Integer i:list){
System.out.print(i+" ");
}
System.out.println();
}
if(!isLeaf){
if(pRoot.rightNode != null){
findPath(pRoot.leftNode,exceptedSum,list,currentSum);
}
if(pRoot.rightNode != null){
findPath(pRoot.rightNode,exceptedSum,list,currentSum);
}
}
list.remove(list.size() - 1);
}
}
public class Clone {
public ComplexListNode clone(ComplexListNode pHead){
cloneNodes(pHead);
connectingSiblingNodes(pHead);
return reconnectNodes(pHead);
}
public void cloneNodes(ComplexListNode pHead){
ComplexListNode pNode = pHead;
while(pNode != null){
ComplexListNode copyNode = new ComplexListNode();
copyNode.pValue = pNode.pValue;
copyNode.pNext = pNode.pNext;
copyNode.pSibling = null;
pNode.pNext = copyNode;
pNode = copyNode.pNext;
}
}
public void connectingSiblingNodes(ComplexListNode pHead){
ComplexListNode pNode = pHead;
while(pNode != null){
ComplexListNode cloneNode = pNode.pNext;
if(pNode.pSibling != null){
cloneNode.pSibling = pNode.pSibling.pNext;
}
pNode = pNode.pNext.pNext;
}
}
public ComplexListNode reconnectNodes(ComplexListNode pHead){
ComplexListNode pNode = pHead;
ComplexListNode cloneHead = pHead.pNext;
ComplexListNode cloneNode = cloneHead;
while(cloneNode.pNext != null){
pNode.pNext = cloneNode.pNext;
pNode = pNode.pNext;
cloneNode.pNext = pNode.pNext;
cloneNode = cloneNode.pNext;
}
pNode.pNext = null;
cloneNode.pNext = null;
return cloneHead;
}
}