Home > Reflections | ā®ļø āļø
2024-06-26
šļø Coding Practice
545.Ā Boundary of Binary Tree
TheĀ boundaryĀ of a binary tree is the concatenation of theĀ root, theĀ left boundary, theĀ leavesĀ ordered from left-to-right, and theĀ reverse orderĀ of theĀ right boundary.
TheĀ left boundaryĀ is the set of nodes defined by the following:
- The root nodeās left child is in the left boundary. If the root does not have a left child, then the left boundary isĀ empty.
- If a node in the left boundary and has a left child, then the left child is in the left boundary.
- If a node is in the left boundary, hasĀ noĀ left child, but has a right child, then the right child is in the left boundary.
- The leftmost leaf isĀ notĀ in the left boundary.
TheĀ right boundaryĀ is similar to theĀ left boundary, except it is the right side of the rootās right subtree. Again, the leaf isĀ notĀ part of theĀ right boundary, and theĀ right boundaryĀ is empty if the root does not have a right child.
TheĀ leavesĀ are nodes that do not have any children. For this problem, the root isĀ notĀ a leaf.
Given theĀroot
Ā of a binary tree, returnĀ the values of itsĀ boundary.
šŖ Reflections
- 52 minutes is a long time
- 8:20 is too long for planning
- finishing the first implementation in ~12 minutes isnāt bad, but there were a couple of bugs
- testing took a long time, but it found several bugs
- But this is a pretty complicate problem, so maybe itās not too bad
- Iām happy with the bugs I caught during manual testing
- The bug I didnāt catch during manual testing was a careless mistake
- I didnāt think about ordering the nodes properly on my first pass of the high-level implementation
- and I didnāt notice that my test result was not what it should have been (in terms of the order of nodes) at the end of testing
- but I was able to recognize the issue immediately and fixed it in one shot
- Iām happy with my recursive implementations
- I feel like working with trees is getting more natural
āØļø My Solutions
/* Return the values of the boundary of the tree
Boundary =
1. root
2. left boundary (left subtree of root + leftmost, non-leaf nodes in left subtree)
3. leaves
4. right boundary (analogous to left boundary)
Approaches:
1. walk the tree 3 times to get the left boundary, right boundary, and leaves - O(N)
2. walk the tree once, pushing elements into either left boundary, right boundary, or leaf arrays - O(N)
Approach 1 is a bit slower due to multiple passes, but it may be simpler code, thus better in an interview context.
[8:20] Done planning
[20:58] Done with initial implementation
Bugs found during manual testing:
1. left boundary: including inner right nodes
[31:01] done fixing leftBoundary
2. leafNodes: error detecting root node
[37:17] done fixing leafNodes
[44:57] done with test
Bugs found during null checks
1. pushed node instead of node.val into nodes array in leafNodes function
[48:20] done fixing leafNodes
[49:05] first automated test
Wrong Answer: [1,null,2,3,4] -> [1,2,3,4] (expected [1,3,4,2])
Ah.. correct nodes, wrong order
[52:30] Fixed
[52:37] Successful submission
*/
/**
* Definition for a binary tree node.
* class TreeNode {
* val: number
* left: TreeNode | null
* right: TreeNode | null
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
* }
*/
const isLeaf = (node: TreeNode): boolean => !node.left && !node.right
function leftBoundary(root: TreeNode, boundary: number[] = []): number[] { // [2,4,5,null,null,7,8]; [1,2,3,4,5,6,null,null,null,7,8,9,10]
if (root.left) {
if (isLeaf(root.left)) {
return boundary // [2]
} else {
boundary.push(root.left.val) // [2]
leftBoundary(root.left, boundary) //
}
} else if (boundary.length && root.right && !isLeaf(root.right)) {
boundary.push(root.right.val)
leftBoundary(root.right, boundary)
}
return boundary
}
function rightBoundary(root: TreeNode, boundary: number[] = []): number[] { // [6,9,10]; [3,6,null,9,10]; [1,2,3,4,5,6,null,null,null,7,8,9,10]
if (root.right) {
if (isLeaf(root.right)) {
return boundary // [3,6]
} else {
boundary.push(root.right.val) // [3]
rightBoundary(root.right, boundary) //
}
} else if (boundary.length && root.left && !isLeaf(root.left)) {
boundary.push(root.left.val) // [3,6]
rightBoundary(root.left, boundary) //
}
return boundary
}
function leafNodes(root: TreeNode, nodes: number[] = [], first = true): number[] { // [10]; [9]; [6,9,10]; [3,6,null,9,10]; [8]; [7]; [5,7,8]; [4]; [2,4,5,null,null,7,8]; [1,2,3,4,5,6,null,null,null,7,8,9,10]
if (!first && isLeaf(root)) {
nodes.push(root.val) // [4,7,8,9,10]; [4,7,8,9]; [4,7,8]; [4,7]; [4]
}
if (root.left) leafNodes(root.left, nodes, false) // // // // //
if (root.right) leafNodes(root.right, nodes, false) // // // //
return nodes // [4,7,8,9,10]
}
function boundaryOfBinaryTree(root: TreeNode | null): number[] { // [1,2,3,4,5,6,null,null,null,7,8,9,10]
if (!root) return []
const boundary: number[] = [
root.val, // 1
...leftBoundary(root), // [2]
...leafNodes(root), // [4,7,8,9,10]
...rightBoundary(root).reverse(), // Correct: [6,3]; Bug: [3,6]
]
return boundary // Correct: [1,2,4,7,8,9,10,6,3]; Bug: [1,2,3,6,4,7,8,9,10]
};