Simple Java solution


  • 0
    U
    class Solution {
        int max = 0;
        List<Integer> begin = new ArrayList<>();
        
        public int widthOfBinaryTree(TreeNode root) {
            travel(root, 1, 0);
            return max;
        }
        
        void travel(TreeNode root, int width, int level) {
            if (root != null) {
                if (begin.size() <= level) {
                    begin.add(width);
                }
                max = Math.max(max, width - begin.get(level) + 1);
                travel(root.left, width*2-1, level+1);
                travel(root.right, width*2, level+1);
            }
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.