给定2个数组C[],D[]
c是城市d是城市价值:
C[0] = 1 D[0] = 6
C[1] = 3 D[1] = 2
C[2] = 0 D[2] = 7
C[3] = 3 D[3] = 5
C[4] = 2 D[4] = 6
C[5] = 4 D[5] = 5
C[6] = 4 D[6] = 2
城市c [0] = 1城市#0与城市#1和城市0的价值= 6相关联, 和城市c [1] = 3城市#1与城市#3和城市1的价值= 2相关联, 依此类推,我在二叉树中代表它们,但我怎样才能使具有最大价值的城市成为我旋转树时保持关系的根。
目标是具有最大值且仅与此相关联的城市的计数数量 代码:
class BinaryTree {
Node root;
public void addNode(int toCity, int name, int atractiveness) {
Node newNode = new Node(toCity, name, atractiveness);
if (root == null) {
root = newNode;
} else {
Node focusNode = root;
Node parent;
while (true) {
parent = focusNode;
if (toCity < focusNode.toCity) {
focusNode = focusNode.leftChild;
if (focusNode == null) {
parent.leftChild = newNode;
return;
}
} else {
focusNode = focusNode.rightChild;
if (focusNode == null) {
parent.rightChild = newNode;
return; // All Done
}
}
}
}
}
public void preorderTraverseTree(Node focusNode) {
int totalNumOfCities=0;
int totalAtractivness=0;
if (focusNode != null) {
System.out.println(focusNode);
preorderTraverseTree(focusNode.leftChild);
preorderTraverseTree(focusNode.rightChild);
}
}
public Node findNode(int name) {
Node focusNode = root;
while (focusNode.name != name) {
if (name < focusNode.name) {
focusNode = focusNode.leftChild;
} else {
focusNode = focusNode.rightChild;
}
if (focusNode == null)
return null;
}
return focusNode;
}
}
public int solution(int K, int[] C, int[] D){
BinaryTree theTree = new BinaryTree();
for (int i = 0; i < C.length; i++) {
theTree.addNode(C[i], i,D[i]);
}
theTree.preorderTraverseTree(theTree.root);
return 0;
}
}