0
0
DSA Javascriptprogramming~20 mins

Tree Traversal Inorder Left Root Right in DSA Javascript - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Inorder Traversal Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of inorder traversal?
Given the binary tree below, what is the output of inorder traversal (Left, Root, Right)?
DSA Javascript
class Node {
  constructor(value) {
    this.value = value;
    this.left = null;
    this.right = null;
  }
}

const 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);

function inorder(node, result = []) {
  if (!node) return;
  inorder(node.left, result);
  result.push(node.value);
  inorder(node.right, result);
  return result;
}

console.log(inorder(root));
A[3, 5, 7, 10, 15]
B[10, 5, 3, 7, 15]
C[15, 10, 7, 5, 3]
D[5, 3, 7, 10, 15]
Attempts:
2 left
💡 Hint
Remember inorder means visit left child first, then root, then right child.
Predict Output
intermediate
1:00remaining
Output of inorder traversal on a single node tree
What is the output of inorder traversal on a tree with only one node with value 42?
DSA Javascript
class Node {
  constructor(value) {
    this.value = value;
    this.left = null;
    this.right = null;
  }
}

const root = new Node(42);

function inorder(node, result = []) {
  if (!node) return;
  inorder(node.left, result);
  result.push(node.value);
  inorder(node.right, result);
  return result;
}

console.log(inorder(root));
A[42]
B[]
C[null]
D[0]
Attempts:
2 left
💡 Hint
A single node tree returns just that node's value in inorder.
🔧 Debug
advanced
2:30remaining
Find the error in inorder traversal function
The following code is intended to perform inorder traversal but produces incorrect output. What is the error?
DSA Javascript
function inorder(node, result = []) {
  if (!node) return result;
  inorder(node.left, result);
  result.push(node.value);
  inorder(node.right, result);
  return result;
}

const root = { value: 1, left: null, right: null };
console.log(inorder(root));
AThe function should push node.value before calling inorder on left child.
BThe function does not check if node.left or node.right exist before calling inorder.
CThe base case returns result instead of undefined, causing double returns.
DThe function should not return result at all.
Attempts:
2 left
💡 Hint
Check what happens when node is null and what the function returns.
Predict Output
advanced
2:30remaining
Inorder traversal output for unbalanced tree
What is the output of inorder traversal for the following unbalanced tree?
DSA Javascript
class Node {
  constructor(value) {
    this.value = value;
    this.left = null;
    this.right = null;
  }
}

const root = new Node(8);
root.right = new Node(10);
root.right.right = new Node(14);
root.right.right.left = new Node(13);

function inorder(node, result = []) {
  if (!node) return;
  inorder(node.left, result);
  result.push(node.value);
  inorder(node.right, result);
  return result;
}

console.log(inorder(root));
A[8, 10, 14, 13]
B[14, 13, 10, 8]
C[10, 8, 13, 14]
D[8, 10, 13, 14]
Attempts:
2 left
💡 Hint
Remember inorder visits left subtree first, then root, then right subtree.
🧠 Conceptual
expert
1:30remaining
How many nodes are visited in inorder traversal of a full binary tree with height 3?
Consider a full binary tree where every node has 0 or 2 children. If the tree height is 3 (root at height 1), how many nodes will inorder traversal visit?
A3
B7
C15
D8
Attempts:
2 left
💡 Hint
A full binary tree of height h has 2^h - 1 nodes.