LeetCode 545: Boundary of Binary Tree Solution
Master LeetCode problem 545 (Boundary of Binary Tree), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.
545. Boundary of Binary Tree
Problem Explanation
Explanation:
The problem "Boundary of Binary Tree" asks us to return the boundary of a binary tree in anti-clockwise order starting from the root. The boundary of a binary tree consists of four parts: the left boundary, the leaves, the right boundary, and the root.
To solve this problem, we can follow these steps:
- Traverse the left boundary in a top-down manner until we reach the leftmost leaf node.
- Traverse the leaf nodes from left to right.
- Traverse the right boundary in a bottom-up manner starting from the rightmost leaf node.
- Construct the boundary by combining these three parts.
Time Complexity: O(n) where n is the number of nodes in the binary tree. Space Complexity: O(n) for the recursive function calls and the output boundary list.
:
Solution Code
class Solution {
public List<Integer> boundaryOfBinaryTree(TreeNode root) {
List<Integer> boundary = new ArrayList<>();
if (root == null) {
return boundary;
}
boundary.add(root.val);
if (root.left != null) {
getLeftBoundary(root.left, boundary);
}
getLeaves(root, boundary);
if (root.right != null) {
getRightBoundary(root.right, boundary);
}
return boundary;
}
private void getLeftBoundary(TreeNode node, List<Integer> boundary) {
if (node == null || (node.left == null && node.right == null)) {
return;
}
boundary.add(node.val);
if (node.left == null) {
getLeftBoundary(node.right, boundary);
} else {
getLeftBoundary(node.left, boundary);
}
}
private void getLeaves(TreeNode node, List<Integer> boundary) {
if (node == null) {
return;
}
if (node.left == null && node.right == null) {
boundary.add(node.val);
}
getLeaves(node.left, boundary);
getLeaves(node.right, boundary);
}
private void getRightBoundary(TreeNode node, List<Integer> boundary) {
if (node == null || (node.left == null && node.right == null)) {
return;
}
if (node.right == null) {
getRightBoundary(node.left, boundary);
} else {
getRightBoundary(node.right, boundary);
}
boundary.add(node.val);
}
}Try It Yourself
Loading code editor...
Related LeetCode Problems
Frequently Asked Questions
How to solve LeetCode 545 (Boundary of Binary Tree)?
This page provides optimized solutions for LeetCode problem 545 (Boundary of Binary Tree) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.
What is the time complexity of LeetCode 545 (Boundary of Binary Tree)?
The time complexity for LeetCode 545 (Boundary of Binary Tree) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.
Can I run code for LeetCode 545 on DevExCode?
Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 545 in Java, C++, or Python.