简体   繁体   中英

Java: Find Height of N-ary Tree

There's lots on finding the height of binary trees; how can the height of a n-ary tree be found in Java? Would it involve traversing it and keeping track of max depth or something like that?

Finding the height is in fact no different for N-ary trees than for any other type of tree. A leaf node has height 0, and a non-leaf node has height one more than the tallest of its children.

Have a recursive function that in pseudocode does this in Java:

public static int getHeight(Node n){
    if(n.isLeaf()){
         return 0;
    }else{
        int maxDepth = 0;

        foreach(Node child : n.getChildren()){
            maxDepth = Math.max(maxDepth, getHeight(child));
        }

        return maxDepth + 1;
    }
}

I'm 4 years and 9 months late to this party but I think the above pseudo-code is pretty much correct, but this:

if(n.isLeaf()){
     return 0;
}

Should be:

if(n.isLeaf()){
     return 1;
}

Here is a working solution to the problem:

public int dfsForMaxDepth(Node node) {

    if (node == null) {
        return 0;
    }

    if (node.children.size() == 0) {
        return 1;
    }

    int maxDepth = 0;

    for (int i = 0; i < node.children.size(); i++) {
        maxDepth = Math.max(maxDepth, dfsForMaxDepth(node.children.get(i)));
    }

    return maxDepth + 1;
}

Base Case 1

If the root is null the max depth is 0.

Base Case 2

If the node has no children (is a leaf) then the height at that node is simply 1.

Recursive Case

If the node does have children we find the max depth for all of its children and "compete" all of those results against each other and finally return the "winning" max depth from all of the children nodes and also count the node that we are at.

Base case 2 could be removed because if the node has no children then the for loop would never have any "room" to run and would just return maxDepth + 1 but since maxDepth with no search is 0, 0 + 1 = 1 which gives you the same things as the logic:

if (node.children.size() == 0) {
    return 1;
}

Fundamental Tip

Tree problems can be confusing because we try to do too much. We try to store extra state that recursion should handle for us or we just overthink what needs to be happening.

The key is that you let the recursion do the heavy lifting and only think "What needs to happen HERE. What needs to happen at this node." (of course in production you'd often convert the recursive DFS into an iterative solution with a stack-like structure since you may be limited by system memory but the recursive solution is more elegant and easier to see in an interview)

So it is 2 things:

1.) What is the goal for this function? Really understand what you want out of the function

2.) What is the tiny puzzle piece that each call will fill to form the whole picture at the end of it all?

Anyway, hope that helps future people coming to this question.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM