1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| public Node getNearsetFarther(Node root,Node node1,Node node2){ if(checkNode(node1, node2)){ return node1; } if(checkNode(node2, node1)){ return node2; } boolean oneInLeft,oneInRight,twoInLeft,twoInRight; oneInLeft=checkNode(root.leftChild, node1); oneInRight=checkNode(root.rightChild, node1); twoInLeft=checkNode(root.leftChild,node2); twoInRight=checkNode(root.rightChild, node2);
if((oneInLeft&&twoInRight)||(oneInRight&&oneInLeft)){ return root; } if(oneInLeft&&twoInLeft){ return getNearsetFarther(root.leftChild, node1, node2); } if(oneInRight&&twoInRight){ return getNearsetFarther(root.rightChild, node1, node2); } else{ return null; } }
|