0
0
DSA Goprogramming~10 mins

Height 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 return 0 when the node is nil.

DSA Go
func height(node *Node) int {
    if node == [1] {
        return 0
    }
    return 1
}
Drag options to blanks, or click blank then click option'
Anil
B0
Cnode
Dtrue
Attempts:
3 left
💡 Hint
Common Mistakes
Returning 1 instead of 0 for nil node
Using 0 instead of nil in condition
2fill in blank
medium

Complete the code to recursively calculate the height of the left subtree.

DSA Go
leftHeight := height([1])
Drag options to blanks, or click blank then click option'
Anil
Bnode.right
Cnode
Dnode.left
Attempts:
3 left
💡 Hint
Common Mistakes
Using node.right instead of node.left
Passing node instead of node.left
3fill in blank
hard

Fix the error in the code to return the maximum height between left and right subtrees plus one.

DSA Go
return [1](leftHeight, rightHeight) + 1
Drag options to blanks, or click blank then click option'
Amin
Bsum
Cmax
Davg
Attempts:
3 left
💡 Hint
Common Mistakes
Using min instead of max
Adding heights instead of taking max
4fill in blank
hard

Fill both blanks to calculate left and right subtree heights correctly.

DSA Go
leftHeight := height([1])
rightHeight := height([2])
Drag options to blanks, or click blank then click option'
Anode.left
Bnode.right
Cnode
Dnil
Attempts:
3 left
💡 Hint
Common Mistakes
Using node for both blanks
Using nil instead of child nodes
5fill in blank
hard

Fill all three blanks to complete the height function using recursion and max function.

DSA Go
func height(node *Node) int {
    if node == [1] {
        return 0
    }
    leftHeight := height([2])
    rightHeight := height([3])
    return max(leftHeight, rightHeight) + 1
}
Drag options to blanks, or click blank then click option'
Anil
Bnode.left
Cnode.right
Dnode
Attempts:
3 left
💡 Hint
Common Mistakes
Not checking for nil node
Using node instead of node.left or node.right