打印三叉树的元素

huangapple go评论76阅读模式
英文:

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 + &quot; &quot;);
    printNodes(node, currentDepth, targetDepth);
}

public static void printNodes(int node, int currentDepth, int targetDepth) {
    if (targetDepth == 1) {
        System.out.print((3 * node + 1) + &quot; &quot; + (3 * node + 2) + &quot; &quot; + (3 * node + 3));
        return;
    }
    if (currentDepth &lt; targetDepth) {
        int leftChild, midChild, rightChild;
        int rightParent = 3 * node + 3;
        node++;
        while (node &lt;= rightParent) {
            leftChild = 3 * node + 1;
            midChild = 3 * node + 2;
            rightChild = 3 * node + 3;
            System.out.print(node + &quot; &quot; + leftChild + &quot; &quot; + midChild + &quot; &quot; + rightChild + &quot; &quot;);
            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&#39;s children onto the stack(from right to left)

when stack is empty, you got your sequence!

huangapple
  • 本文由 发表于 2020年10月20日 05:36:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/64435389.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定