0
0
DSA Goprogramming~20 mins

Diameter of Binary Tree in DSA Go - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Diameter Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Diameter Calculation for a Simple Tree
What is the output of the following Go code that calculates the diameter of a binary tree?
DSA Go
package main
import "fmt"
type TreeNode struct {
    Val int
    Left *TreeNode
    Right *TreeNode
}

func diameterOfBinaryTree(root *TreeNode) int {
    maxDiameter := 0
    var depth func(node *TreeNode) int
    depth = func(node *TreeNode) int {
        if node == nil {
            return 0
        }
        left := depth(node.Left)
        right := depth(node.Right)
        if left+right > maxDiameter {
            maxDiameter = left + right
        }
        if left > right {
            return left + 1
        }
        return right + 1
    }
    depth(root)
    return maxDiameter
}

func main() {
    root := &TreeNode{Val: 1}
    root.Left = &TreeNode{Val: 2}
    root.Right = &TreeNode{Val: 3}
    root.Left.Left = &TreeNode{Val: 4}
    root.Left.Right = &TreeNode{Val: 5}
    fmt.Println(diameterOfBinaryTree(root))
}
A2
B4
C3
D5
Attempts:
2 left
💡 Hint
Think about the longest path between any two nodes in the tree.
🧠 Conceptual
intermediate
1:30remaining
Understanding Diameter Definition
Which of the following best describes the diameter of a binary tree?
AThe number of edges on the longest path between any two nodes in the tree.
BThe height of the tree from the root node to the deepest leaf.
CThe total number of nodes in the tree.
DThe number of nodes on the longest path between any two nodes in the tree.
Attempts:
2 left
💡 Hint
Diameter counts edges, not nodes.
🔧 Debug
advanced
2:00remaining
Identify the Error in Diameter Calculation
What error will occur when running this Go code to calculate the diameter of a binary tree?
DSA Go
package main
import "fmt"
type TreeNode struct {
    Val int
    Left *TreeNode
    Right *TreeNode
}

func diameterOfBinaryTree(root *TreeNode) int {
    maxDiameter := 0
    var depth func(node *TreeNode) int
    depth = func(node *TreeNode) int {
        if node == nil {
            return 0
        }
        left := depth(node.Left)
        right := depth(node.Right)
        if left+right > maxDiameter {
            maxDiameter = left + right
        }
        return max(left, right) + 1
    }
    depth(root)
    return maxDiameter
}

func max(a, b int) int {
    if a > b {
        return a
    }
    return b
}

func main() {
    root := &TreeNode{Val: 1}
    root.Left = &TreeNode{Val: 2}
    root.Right = &TreeNode{Val: 3}
    fmt.Println(diameterOfBinaryTree(root))
}
AOutput is 2
BRuntime panic: nil pointer dereference
CCompilation error: undefined function max
DOutput is 3
Attempts:
2 left
💡 Hint
Check if the max function is defined and used correctly.
🚀 Application
advanced
2:00remaining
Diameter of Unbalanced Binary Tree
Given the following unbalanced binary tree, what is the diameter?
DSA Go
package main
import "fmt"
type TreeNode struct {
    Val int
    Left *TreeNode
    Right *TreeNode
}

func diameterOfBinaryTree(root *TreeNode) int {
    maxDiameter := 0
    var depth func(node *TreeNode) int
    depth = func(node *TreeNode) int {
        if node == nil {
            return 0
        }
        left := depth(node.Left)
        right := depth(node.Right)
        if left+right > maxDiameter {
            maxDiameter = left + right
        }
        if left > right {
            return left + 1
        }
        return right + 1
    }
    depth(root)
    return maxDiameter
}

func main() {
    root := &TreeNode{Val: 1}
    root.Left = &TreeNode{Val: 2}
    root.Left.Left = &TreeNode{Val: 3}
    root.Left.Left.Left = &TreeNode{Val: 4}
    root.Right = &TreeNode{Val: 5}
    fmt.Println(diameterOfBinaryTree(root))
}
A3
B4
C2
D5
Attempts:
2 left
💡 Hint
Consider the longest path between two nodes, which may not pass through the root.
🧠 Conceptual
expert
1:30remaining
Effect of Diameter Calculation on Tree Traversal Complexity
What is the time complexity of calculating the diameter of a binary tree using the depth-first search approach shown in the code examples?
AO(1), since diameter is calculated using constant time operations
BO(n^2), because depth is calculated multiple times for each node
CO(log n), assuming the tree is balanced
DO(n), where n is the number of nodes in the tree
Attempts:
2 left
💡 Hint
Each node is visited once in the depth-first search.