LeetCode 655: Print Binary Tree Solution

Master LeetCode problem 655 (Print Binary Tree), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

Problem Explanation

Explanation

To solve this problem, we need to construct a formatted layout matrix that visually represents the binary tree. We can achieve this by traversing the tree in a level-order manner and placing each node at the correct position in the matrix based on the specified rules.

  1. Calculate the height of the tree.
  2. Initialize the result matrix with the correct dimensions.
  3. Perform a level-order traversal of the tree and populate the matrix with the nodes.

Time complexity: O(n) where n is the number of nodes in the binary tree. Space complexity: O(n) where n is the number of nodes in the binary tree.

Solution Code

class Solution {
    public List<List<String>> printTree(TreeNode root) {
        int height = getHeight(root);
        int rows = height;
        int cols = (int) Math.pow(2, height) - 1;

        List<List<String>> res = new ArrayList<>();
        for (int i = 0; i < rows; i++) {
            List<String> row = new ArrayList<>();
            for (int j = 0; j < cols; j++) {
                row.add("");
            }
            res.add(row);
        }

        Queue<TreeNode> queue = new LinkedList<>();
        Queue<int[]> indices = new LinkedList<>();
        queue.offer(root);
        indices.offer(new int[]{0, cols - 1});

        int level = 0;
        while (!queue.isEmpty()) {
            int size = queue.size();
            for (int i = 0; i < size; i++) {
                TreeNode node = queue.poll();
                int[] index = indices.poll();
                int mid = (index[0] + index[1]) / 2;
                res.get(level).set(mid, String.valueOf(node.val));

                if (node.left != null) {
                    queue.offer(node.left);
                    indices.offer(new int[]{index[0], mid - 1});
                }
                if (node.right != null) {
                    queue.offer(node.right);
                    indices.offer(new int[]{mid + 1, index[1]});
                }
            }
            level++;
        }

        return res;
    }

    private int getHeight(TreeNode root) {
        if (root == null) {
            return 0;
        }
        return 1 + Math.max(getHeight(root.left), getHeight(root.right));
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 655 (Print Binary Tree)?

This page provides optimized solutions for LeetCode problem 655 (Print Binary Tree) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 655 (Print Binary Tree)?

The time complexity for LeetCode 655 (Print Binary Tree) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 655 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 655 in Java, C++, or Python.

Back to LeetCode Solutions