Traverse every unique path (from root to leaf) in an arbitrary tree structure

前端 未结 4 1248
执念已碎
执念已碎 2021-01-31 12:18

I have several lists:

A = [\"a0\", \"a1\"]       // the number of lists varies
B = [\"b0\", \"b1\", \"b2\"] // such as the number of elements in a list.
C = [\"c         


        
4条回答
  •  余生分开走
    2021-01-31 13:07

    1,Find leaf node
    2,Up traversal from leaf node

    public void printPath(N n) {
            if (n == null)
                return;
            if (n.left == null && n.right == null) {
                do {
                    System.out.print(n.value);
                    System.out.print(" ");
                } while ((n = n.parent) != null);
                System.out.println("");
                return;
            }
            printPath(n.left);
            printPath(n.right);
        }
    

    printPath(Root);

提交回复
热议问题