Boundary of a Binary Tree, Anticlockwise
Updated
Problem statement
Given the root of a binary tree, return the values on its boundary, read anticlockwise starting at the root. The boundary is made of these parts, in this order:
- The root.
- The left edge, top to bottom: start at the root's left child; from each node move to its left child, or to its right child if it has no left child. Stop before reaching a leaf. If the root has no left child, the left edge is empty.
- The leaves (nodes with no children), from left to right. The root is not counted here.
- The right edge, bottom to top: built like the left edge but starting at the root's right child and preferring right children, again stopping before a leaf, then listed in reverse.
Each node appears once. A tree with a single node has the boundary [root.val]. Values may repeat; list each node's value.
Function
boundaryTraversal(root: TreeNode) → List<Integer>Examples
Example 1
root = [6,2,9,1,4,null,11,null,null,3,5,10]return = [6,2,1,3,5,10,11,9]Root 6. The left edge is just 2, because its left child 1 is a leaf. The leaves from left to right are 1, 3, 5, 10. The right edge from the top is 9, 11 (10 is a leaf), so going up it is 11, 9.
Example 2
root = [7,null,3,8,null,2,6]return = [7,2,6,8,3]The root has no left child, so the left edge is empty. The leaves are 2, 6. The right edge starts at 3; 3 has no right child, so the edge goes to its left child 8, and stops above the leaves. Going up, that edge is 8, 3.
Example 3
root = [4]return = [4]A single node is the whole boundary: the root, listed once.
Constraints
- The tree has between
1and2000nodes. -1000 <= node.val <= 1000- The tree does not have to be balanced. It can be a single chain of
2000nodes.
More Adobe Phone Screen questions
- Evaluate a Parenthesized Integer ExpressionPHONE SCREEN · Seen Oct 2026
- All Elements of Two BSTs in OrderPHONE SCREEN · Seen Aug 2026
- Rotate an Array In PlaceOA · PHONE SCREEN · Seen Aug 2026
- Binary Tree Level-Order Traversal by LevelsPHONE SCREEN · ONSITE INTERVIEW · Seen Aug 2026
- Count Occurrences in a Sorted ArrayPHONE SCREEN · ONSITE INTERVIEW · Seen Jul 2026
- Count Pairs with Absolute Difference KPHONE SCREEN · Seen Jul 2026
- Count Subsets with Sum KPHONE SCREEN · Seen Jul 2026
- Numbers with Exactly Three DivisorsPHONE SCREEN · Seen Jul 2026
- Sum of Even Nodes on a Path in a Numbered Binary TreePHONE SCREEN · Seen Jul 2026
- Largest Value in Every Sliding WindowPHONE SCREEN · Seen Jul 2026
Similar questions at other companies
- N-ary Index-Path Encoder and DecoderBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- House Robber IIIOkta · PHONE SCREENOkta interview questions
- Render a Task Tree from CSV RowsStripe · PHONE SCREEN · ONSITE INTERVIEWStripe interview questions
- Validate Binary Search TreeKotak Mahindra Bank · PHONE SCREENKotak Mahindra Bank interview questions
- House Robber IIIGoldman Sachs · PHONE SCREENGoldman Sachs interview questions
- Binary Tree Target-Sum PathsByteDance · PHONE SCREENByteDance interview questions
- House Robber IIIVisa · PHONE SCREENVisa interview questions
- Find All Paths for a Name in a File SystemThe Walt Disney Company · PHONE SCREENThe Walt Disney Company interview questions
- Validate a BST with a Custom ComparisonApple · PHONE SCREENApple interview questions
- Filter Undesired Comments and Their DescendantsReddit · PHONE SCREEN · ONSITE INTERVIEWReddit interview questions