0
0
DSA Goprogramming~10 mins

Diameter of Binary Tree in DSA Go - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define the TreeNode struct with integer value and pointers to left and right children.

DSA Go
type TreeNode struct {
    Val   int
    Left  *[1]
    Right *TreeNode
}
Drag options to blanks, or click blank then click option'
ABinaryNode
BTree
CTreeNode
DNode
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different struct name for the child pointers.
Forgetting the pointer symbol '*' before the struct name.
2fill in blank
medium

Complete the code to calculate the maximum of two integers a and b.

DSA Go
func max(a, b int) int {
    if a [1] b {
        return a
    }
    return b
}
Drag options to blanks, or click blank then click option'
A<
B>
C==
D<=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' causing minimum to be returned.
Using '==' which only checks equality.
3fill in blank
hard

Fix the error in the recursive function to compute the height of a binary tree node.

DSA Go
func height(root *TreeNode) int {
    if root == nil {
        return 0
    }
    leftHeight := height(root.[1])
    rightHeight := height(root.Right)
    if leftHeight > rightHeight {
        return leftHeight + 1
    }
    return rightHeight + 1
}
Drag options to blanks, or click blank then click option'
ARight
Bleft
Cright
DLeft
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase 'left' which causes compilation error.
Confusing 'Right' and 'left' fields.
4fill in blank
hard

Fill both blanks to update the diameter and return the height of the current node.

DSA Go
func diameterHelper(root *TreeNode, diameter *int) int {
    if root == nil {
        return 0
    }
    leftHeight := diameterHelper(root.Left, diameter)
    rightHeight := diameterHelper(root.Right, diameter)
    *diameter = max(*diameter, leftHeight [1] rightHeight)
    return max(leftHeight, rightHeight) [2] 1
}
Drag options to blanks, or click blank then click option'
A+
B*
C-
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using multiplication or subtraction instead of addition.
Returning sum instead of max for height.
5fill in blank
hard

Fill all three blanks to implement the diameter of binary tree function using helper.

DSA Go
func diameterOfBinaryTree(root *TreeNode) int {
    diameter := 0
    var [1] func(*TreeNode) int
    [1] = func(node *TreeNode) int {
        if node == nil {
            return 0
        }
        left := [2](node.Left)
        right := [2](node.Right)
        if left + right > diameter {
            diameter = left + right
        }
        if left > right {
            return left + 1
        }
        return right + 1
    }
    [1](root)
    return diameter
}
Drag options to blanks, or click blank then click option'
Aheight
BdiameterHelper
CheightHelper
DcalcHeight
Attempts:
3 left
💡 Hint
Common Mistakes
Using different names for function definition and calls causing errors.
Not updating diameter correctly inside helper.