LeetCode: Invert Binary Tree Solution
A Broken Interview: The OriginApproach
Classic swap solution
1temp = a2a = b3b = temp
Recursively call the tree branches
Implementation
1var invertTree = function (root) {2 if (!root) return root3 let temp = root.left4 root.left = root.right5 root.right = temp6 invertTree(root.left)7 invertTree(root.right)8 return root9}
References
Comments
Loading comments...
Tags
leetcode
recursion
tree
binary tree
Apply and earn a $2,500 bonus once you're hired on your first job!
Clients from the Fortune 500 to Silicon Valley startups
Choose your own rate, get paid on time
From hourly, part-time, to full-time positions
Flexible remote working environment
A lot of open JavaScript jobs!!
Fact corner: Referred talent are 5x more likely to pass the Toptal screening process than the average applicant.
Still hesitate? Read HoningJS author's guide on dealing with Toptal interview process.
Next Post
LeetCode: Balanced Binary Tree
Modified version of "Maximum Depth of Binary Tree"
Previous Post
LeetCode: Longest Palindrome by Concatenating Two Letter Words
Things get messy sometimes