Challenge - 5 Problems
Tree Terminology Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output of the tree traversal showing root and leaf nodes?
Given the following Java code that builds a simple tree and prints root and leaf nodes, what is the printed output?
DSA Java
class Node { int value; Node left, right; Node(int val) { value = val; left = right = null; } } public class TreeTest { public static void main(String[] args) { Node root = new Node(10); root.left = new Node(5); root.right = new Node(15); root.left.left = new Node(3); root.left.right = new Node(7); root.right.right = new Node(18); System.out.print("Root: " + root.value + "\n"); System.out.print("Leaf nodes: "); printLeaves(root); } static void printLeaves(Node node) { if (node == null) return; if (node.left == null && node.right == null) { System.out.print(node.value + " "); } printLeaves(node.left); printLeaves(node.right); } }
Attempts:
2 left
💡 Hint
Leaf nodes have no children (left and right are null).
✗ Incorrect
The root node is 10. Leaf nodes are nodes with no children: 3, 7, and 18.
❓ Predict Output
intermediateWhat is the height of the given binary tree?
Consider the binary tree constructed below. What is the height of the tree?
DSA Java
class Node { int value; Node left, right; Node(int val) { value = val; left = right = null; } } public class TreeHeight { public static void main(String[] args) { Node root = new Node(1); root.left = new Node(2); root.right = new Node(3); root.left.left = new Node(4); root.left.left.left = new Node(5); System.out.println(height(root)); } static int height(Node node) { if (node == null) return -1; int leftHeight = height(node.left); int rightHeight = height(node.right); return 1 + Math.max(leftHeight, rightHeight); }
Attempts:
2 left
💡 Hint
Height counts edges on longest path from node to leaf.
✗ Incorrect
Height is number of edges on longest path from root to leaf. Here path is 1->2->4->5 with 3 edges.
❓ Predict Output
advancedWhat is the depth of node with value 7 in the tree?
Given the tree below, what is the depth of the node with value 7?
DSA Java
class Node { int value; Node left, right; Node(int val) { value = val; left = right = null; } } public class TreeDepth { public static void main(String[] args) { Node root = new Node(10); root.left = new Node(5); root.right = new Node(15); root.left.left = new Node(3); root.left.right = new Node(7); System.out.println(depth(root, 7, 0)); } static int depth(Node node, int val, int currentDepth) { if (node == null) return -1; if (node.value == val) return currentDepth; int left = depth(node.left, val, currentDepth + 1); if (left != -1) return left; return depth(node.right, val, currentDepth + 1); }
Attempts:
2 left
💡 Hint
Depth counts edges from root to the node.
✗ Incorrect
Node 7 is two edges away from root: 10->5->7, so depth is 2.
🧠 Conceptual
advancedWhich statement correctly describes the level of nodes in a tree?
Select the correct statement about the level of nodes in a tree.
Attempts:
2 left
💡 Hint
Level counts how far a node is from the root in steps.
✗ Incorrect
Level starts at 0 for root and increases by 1 for each child level down.
🧠 Conceptual
expertWhich node is both a leaf and has height 0 in a tree?
In a tree, which node is guaranteed to be both a leaf and have height 0?
Attempts:
2 left
💡 Hint
Height 0 means no edges below the node.
✗ Incorrect
Leaf nodes have no children and thus height 0 because no edges go down from them.
