function maxDepth(node):
if node is null:
return 0
left = maxDepth(node.left)
right = maxDepth(node.right)
return max(left, right) + 1
📌 2. [101] Symmetric Tree (Check if Mirror)
function isSymmetric(root):
return isMirror(root.left, root.right)
function isMirror(left, right):
if left == null and right == null:
return true
if left == null or right == null:
return false
if left.val != right.val:
return false
return isMirror(left.left, right.right) and isMirror(left.right, right.left)
📌 3. [100] Same Tree
function isSameTree(p, q):
if p == null and q == null:
return true
if p == null or q == null:
return false
if p.val != q.val:
return false
return isSameTree(p.left, q.left) and isSameTree(p.right, q.right)
📌 4. [112] Path Sum
function hasPathSum(node, target):
if node == null:
return false
if node is leaf and node.val == target:
return true
return hasPathSum(node.left, target - node.val) or hasPathSum(node.right, target - node.val)
Top comments (0)