Problem List

Same Tree

May 30, 2025Go binary tree, depth first search, breadth first searcheasy

Problem

Approach

Reflections

Fairly simple DFS problem in all honesty. One issue I did recognize fairly quickly was that I was using one truthy value for the left and right side, so if right was true it would return true, even when left was false.

Performance

Complexity

Go Solution
func isSameTree(p * TreeNode, q * TreeNode) bool {
  if p == nil && q == nil {
    return true
  }

  if p == nil || q == nil {
    return false
  }

  if p.Val != q.Val {
    return false
  }

  left:= isSameTree(p.Left, q.Left)
  right:= isSameTree(p.Right, q.Right)

  return left && right
}
LeetCode Problem Link