Skip to main content
qualify the solution
Source Link
rolfl
  • 98k
  • 17
  • 219
  • 419

The most logical solution to this problem is to do a BFS (Breadth-First-search) and then determine the size of each level as you get to it.

Your solution uses a depth-first search and tracks the results in an array. But, to create the array, it has to calculate the depth too.

If you have to do it that way, then you should calculate the values in an ArrayList instead, and avoid the height calculation first.

The breadth-first algorithm would be simple:

public int getMaxWidth(Node root) {
    Queue<Node> row = new LinkedList<>();
    row.add(root);
    row.add(null); // use null as a marker for the end of a row.
    int maxwidth = 1;
    while (!row.isEmpty()) {
        Node n = row.remove();
        if (n == null) {
            // next row
            int rowsize = row.size();
            if (rowsize > 0) {
                if (rowsize > maxwidth) {
                    maxwidth = rowsize;
                }
                row.add(null); // add new end-of-row-marker.
            }
        } else {
            if (n.left != null) {
                row.add(n.left);
            }
            if (n.right != null) {
                row.add(n.right);
            }
        }
    }
    return maxwidth;
}
    

This way you are restricted to a simple, single scan of your tree, with just a single row's worth of data pointers as memory overhead. \$O(n)\$ time and space complexity.

The most logical solution to this problem is to do a BFS (Breadth-First-search) and then determine the size of each level as you get to it.

Your solution uses a depth-first search and tracks the results in an array. But, to create the array, it has to calculate the depth too.

If you have to do it that way, then you should calculate the values in an ArrayList instead, and avoid the height calculation first.

The algorithm would be simple:

public int getMaxWidth(Node root) {
    Queue<Node> row = new LinkedList<>();
    row.add(root);
    row.add(null); // use null as a marker for the end of a row.
    int maxwidth = 1;
    while (!row.isEmpty()) {
        Node n = row.remove();
        if (n == null) {
            // next row
            int rowsize = row.size();
            if (rowsize > 0) {
                if (rowsize > maxwidth) {
                    maxwidth = rowsize;
                }
                row.add(null); // add new end-of-row-marker.
            }
        } else {
            if (n.left != null) {
                row.add(n.left);
            }
            if (n.right != null) {
                row.add(n.right);
            }
        }
    }
    return maxwidth;
}
    

This way you are restricted to a simple, single scan of your tree, with just a single row's worth of data pointers as memory overhead. \$O(n)\$ time and space complexity.

The most logical solution to this problem is to do a BFS (Breadth-First-search) and then determine the size of each level as you get to it.

Your solution uses a depth-first search and tracks the results in an array. But, to create the array, it has to calculate the depth too.

If you have to do it that way, then you should calculate the values in an ArrayList instead, and avoid the height calculation first.

The breadth-first algorithm would be simple:

public int getMaxWidth(Node root) {
    Queue<Node> row = new LinkedList<>();
    row.add(root);
    row.add(null); // use null as a marker for the end of a row.
    int maxwidth = 1;
    while (!row.isEmpty()) {
        Node n = row.remove();
        if (n == null) {
            // next row
            int rowsize = row.size();
            if (rowsize > 0) {
                if (rowsize > maxwidth) {
                    maxwidth = rowsize;
                }
                row.add(null); // add new end-of-row-marker.
            }
        } else {
            if (n.left != null) {
                row.add(n.left);
            }
            if (n.right != null) {
                row.add(n.right);
            }
        }
    }
    return maxwidth;
}
    

This way you are restricted to a simple, single scan of your tree, with just a single row's worth of data pointers as memory overhead. \$O(n)\$ time and space complexity.

Source Link
rolfl
  • 98k
  • 17
  • 219
  • 419

The most logical solution to this problem is to do a BFS (Breadth-First-search) and then determine the size of each level as you get to it.

Your solution uses a depth-first search and tracks the results in an array. But, to create the array, it has to calculate the depth too.

If you have to do it that way, then you should calculate the values in an ArrayList instead, and avoid the height calculation first.

The algorithm would be simple:

public int getMaxWidth(Node root) {
    Queue<Node> row = new LinkedList<>();
    row.add(root);
    row.add(null); // use null as a marker for the end of a row.
    int maxwidth = 1;
    while (!row.isEmpty()) {
        Node n = row.remove();
        if (n == null) {
            // next row
            int rowsize = row.size();
            if (rowsize > 0) {
                if (rowsize > maxwidth) {
                    maxwidth = rowsize;
                }
                row.add(null); // add new end-of-row-marker.
            }
        } else {
            if (n.left != null) {
                row.add(n.left);
            }
            if (n.right != null) {
                row.add(n.right);
            }
        }
    }
    return maxwidth;
}
    

This way you are restricted to a simple, single scan of your tree, with just a single row's worth of data pointers as memory overhead. \$O(n)\$ time and space complexity.