Leetcode - 3Sum With Multiplicity
Leetcode - Flatten a Multilevel Doubly Linked List

Leetcode - Maximum Width of Binary Tree

violet posted @ Jul 10, 2020 12:28:03 AM in 算法 with tags Algorithm tree Golang , 238 阅读

https://leetcode.com/problems/maximum-width-of-binary-tree/

Given a binary tree, write a function to get the maximum width of the given tree. The width of a tree is the maximum width among all levels. The binary tree has the same structure as a full binary tree, but some nodes are null.

The width of one level is defined as the length between the end-nodes (the leftmost and right most non-null nodes in the level, where the null nodes between the end-nodes are also counted into the length calculation.

Example 1:

Input: 

           1
         /   \
        3     2
       / \     \  
      5   3     9 

Output: 4
Explanation: The maximum width existing in the third level with the length 4 (5,3,null,9).

 

/**
 * Definition for a binary tree node.
 * type TreeNode struct {
 *     Val int
 *     Left *TreeNode
 *     Right *TreeNode
 * }
 */
func widthOfBinaryTree(root *TreeNode) int {
    if root == nil {
        return 0
    }
    queue := []*TreeNode{root}
    result := 0
    for len(queue) != 0 {
        size := len(queue)
        var first int
        var last int
        for first = 0; first < size; first++ {
            node := queue[first]
            if node != nil {
                break
            }
        }
        if first >= size {
            break
        }
        for last = size-1; last >= first; last-- {
            node := queue[last]
            if node != nil {
                break
            }
        }
        for i := first; i <= last; i++ {
            node := queue[i]
            if node != nil {
                queue = append(queue, node.Left)
                queue = append(queue, node.Right)
            } else {
                queue = append(queue, nil)
                queue = append(queue, nil)
            }
        }
        result = max(result, last - first+ + 1)
        queue = queue[size:]
    }
    return result
}

func max(a, b int) int {
    if a > b {
        return a
    }
    return b
}
Independent Delhi Es 说:
Aug 12, 2021 09:18:27 PM

This is very interesting, You are a very skilled blogger. I’ve joined your RSS feed and look forward to seeking more of your great post. Also, I’ve shared your website on my social networks!

Parul 说:
Nov 07, 2021 02:48:12 PM
It has a decent importance. In the event that you in every case live emphatically, Escorts Service in Goa Goa Escorts  Goa Escort Escorts in Goa  some time or another beneficial things will occur. How about we have faith in the force of inspiration. Have a decent day.
WiFi Speed Test 说:
Feb 04, 2023 02:11:00 PM

Internet is something we all use every second of our lives now with WiFi gadgets, where you can test the speed at any time, and where notifications, texts, and emails stream down like rain, we should always have the best Internet connection with us. WiFi Speed Test We've talked a lot about the importance of having a decent Internet connection, but now it's time for you to take action and see whether your Internet is what it claims to be. We will use SpeedTest by Ooakla to check your WiFi speed, which is equivalent to your Internet connection speed.

boardmodelpaper.com 说:
Jan 25, 2024 11:12:29 PM

Board Model Papers 2024 Download with Suggestions for 10th Class Textbooks 2024 Pdf Download and SSLC New Syllabus Sample Question Paper 2024 and different types of model papers boardmodelpaper.com and question papers for following the website and Arts, Science, Commerce Stream Subject Wise Solved Question Bank for Hindi & English Medium Students with Exam Pattern & Blueprint and subject Wise with 11th & 12th Question Bank 2024 for General & Vocational Course Languages & Subjects Important Question for the above link.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter