英文:
Print elements of a ternary tree
问题
class Main {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
int targetDepth = Integer.parseInt(reader.readLine());
int currentDepth = 1;
int node = 0;
System.out.print(node + " ");
printNodes(node, currentDepth, targetDepth);
}
public static void printNodes(int node, int currentDepth, int targetDepth) {
if (targetDepth == 1) {
System.out.print((3 * node + 1) + " " + (3 * node + 2) + " " + (3 * node + 3));
return;
}
if (currentDepth < targetDepth) {
int leftChild, midChild, rightChild;
int rightParent = 3 * node + 3;
node++;
while (node <= rightParent) {
leftChild = 3 * node + 1;
midChild = 3 * node + 2;
rightChild = 3 * node + 3;
System.out.print(node + " " + leftChild + " " + midChild + " " + rightChild + " ");
node++;
}
node = rightParent;
currentDepth++;
printNodes(node, currentDepth, targetDepth);
}
}
}
英文:
A full ternary tree with depth n is given. Write a program that prints all nodes of the tree in Pre-Order. Enumeration starts from 0 and goes sequentially through the levels.
Input: n is the depth of the tree.
Output: a sequence of nodes in Pre-Order divided by spaces.
Sample Input:
2
Sample Output:
0 1 4 5 6 2 7 8 9 3 10 11 12
class Main {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
int targetDepth = Integer.parseInt(reader.readLine());
int currentDepth = 1;
int node = 0;
System.out.print(node + " ");
printNodes(node, currentDepth, targetDepth);
}
public static void printNodes(int node, int currentDepth, int targetDepth) {
if (targetDepth == 1) {
System.out.print((3 * node + 1) + " " + (3 * node + 2) + " " + (3 * node + 3));
return;
}
if (currentDepth < targetDepth) {
int leftChild, midChild, rightChild;
int rightParent = 3 * node + 3;
node++;
while (node <= rightParent) {
leftChild = 3 * node + 1;
midChild = 3 * node + 2;
rightChild = 3 * node + 3;
System.out.print(node + " " + leftChild + " " + midChild + " " + rightChild + " ");
node++;
}
node = rightParent;
currentDepth++;
printNodes(node, currentDepth, targetDepth);
}
}
A simple test reports an error: Invalid answer. The test uses a tree with a depth of 3, although the test passes successfully with a tree depth of 2. I have already checked 100 times, based on the conditions of the problem, the answer must be correct. Perhaps I am missing something. Please point me in the right direction.
答案1
得分: 0
你需要做的是:
public static void process(Node n){
sysout(n.value)
process(n.right)
process(n.mid)
process(n.left)
}
你正在做的是:
public static void process(Node n){
sysout(n.value)
sysout(n.left.value)
sysout(n.mid.value)
sysout(n.right.value)
process(n.right)
}
保持简单,尝试先打印当前节点,然后继续处理其子节点。
如果你正在学习数据结构课程,还有一种使用栈或队列的方法。我建议你也查阅一下这个内容。在使用递归时,你自然会使用到 Java 所使用的进程栈。
伪代码如下:
将初始节点推入栈中
当栈中有节点时:
弹出栈中的节点
打印节点的值
如果节点不在目标深度上:
将节点的子节点从右到左推入栈中
当栈为空时,你就得到了所需的顺序!
英文:
what u need to do is:
public static void process(Node n){
sysout(n.value)
process(n.right)
process(n.mid)
process(n.left)}
what you are doing is:
public static void process(Node n){
sysout(n.value)
sysout(n.left.value)
sysout(n.mid.value)
sysout(n.right.value)
process(n.right)}
keep it simple, just try to print current node and then move on to its children.
If you are currently getting a course on data structures, there is also a method that uses stack or queue. I recommend checking on it too. While using recursion, you naturally use the process stack java uses.
pseudo code goes like:
push initial node into the stack
while stack has nodes:
pop node from the stack
print nodes value
if node is not at the target depth:
push node's children onto the stack(from right to left)
when stack is empty, you got your sequence!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论