Home > Reflections | โฎ๏ธ โญ๏ธ
2024-06-21 | ๐ฟ๐ Terror ๐ | ๐๐ฒ Trees โจ๏ธ
๐ฟ Night Terrors
๐ด I got 8.5 hours of sleep last night.
๐ฉ Yet I woke up feeling like crap.
๐ Our orange tabby is vocal. And demanding.
๐ฅ And this little terrorist has been waking us up in the middle of the night, refusing to go away or shut up until someone feeds him again.
โ On one hand, Iโm glad our little cancer survivor is eating and putting on weight.
โ On the other hand, sleep is critical, and this behavior has been consistent in recent days and weeks.
๐ค What to do?
๐ฎ My wife will know.
โ Rejection
โ๏ธ I heard back from my recruiter today.
๐คซ As is standard, they wonโt provide specific feedback, though I can opt for a phone call with my recruiter.
๐ซ While Iโm surprised that Iโm not continuing past the technical screen, this was definitely a stretch position.
โฌ๏ธ I donโt think Iโll be applying to positions at this level again this time around.
๐๏ธ But the experience was useful, and Iโm still happy with my steady progress and routines in interview preparation.
โญ๏ธ Itโs time to refocus and queue up some more interviews.
๐๏ธ Coding Practice
Revisiting 543.ย Diameter of Binary Tree
I solved this yesterday, but wasnโt satisfied with the solution.
๐ช Reflections
- I didnโt time or manually test any of these, I just wanted to come up with a cleaner solution, not relying on global, mutable state.
- Iโm happy enough with todayโs second solution.
โจ๏ธ My Solutions
/* Refactor depth to its own function
- This can be much less efficient because we may recompute depth multiple times.
- To be precise, we might compute depth for O(N) nodes to determine diameter
so this may be O(N^2) runtime complexity
*/
function depth(root: TreeNode | null): number {
if (!root) return 0
if (!root.left && !root.right) return 0
const leftDepth = root.left ? depth(root.left) + 1 : 0
const rightDepth = root.right ? depth(root.right) + 1 : 0
return Math.max(leftDepth, rightDepth)
}
function diameterOfBinaryTree2(root: TreeNode | null): number {
if (!root) return 0
if (!root.left && !root.right) return 0
const leftDepth = root.left ? depth(root.left) + 1 : 0
const rightDepth = root.right ? depth(root.right) + 1 : 0
const diameter = leftDepth + rightDepth
let maxDiameter = diameter
const queue = [root.left, root.right]
while (queue.length) {
const node = queue.pop()
const d = depth(node)
if (d * 2 > maxDiameter) {
const dL = depth(node.left)
const dR = depth(node.right)
const thisDiameter = dL + dR + 2
if (thisDiameter > maxDiameter) maxDiameter = thisDiameter
if (node.left) queue.push(node.left)
if (node.right) queue.push(node.right)
}
}
return maxDiameter
};
/*
To calculate diameter, we need to know
- depth of left and right sub-trees
- diameter of left and right sub-trees
So let's write a function that returns both depth and diameter.
Then we can call this function once and return only the diameter.
*/
function diameterOfBinaryTree(root: TreeNode | null): number {
function depthAndDiameter(root: TreeNode | null): [number, number] {
if (!root) return [0, 0]
if (!root.left && !root.right) return [0, 0]
const [leftDepth, leftDiameter] = root.left ? depthAndDiameter(root.left) : [0, 0]
const [rightDepth, rightDiameter] = root.right ? depthAndDiameter(root.right) : [0, 0]
const depth = Math.max(leftDepth, rightDepth) + 1
const diameter = (root.left ? leftDepth + 1 : 0) + (root.right ? rightDepth + 1 : 0)
const maxDiameter = Math.max(leftDiameter, rightDiameter, diameter)
return [depth, maxDiameter]
}
const [, diameter] = depthAndDiameter(root)
return diameter
};