Skip to main content
Back to problems
#545
Medium Algorithms

Boundary of binary tree

Tree Depth-First Search Binary Tree
48.0% acceptance
Mar 31, 2026
1420
2347
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 is 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.

Solution

Rust
Time O(n)
Space O(n)
LeetCode
solution.rs
// Definition for a binary tree node.
// #[derive(Debug, PartialEq, Eq)]
// pub struct TreeNode {
//   pub val: i32,
//   pub left: Option<Rc<RefCell<TreeNode>>>,
//   pub right: Option<Rc<RefCell<TreeNode>>>,
// }
// 
// impl TreeNode {
//   #[inline]
//   pub fn new(val: i32) -> Self {
//     TreeNode {
//       val,
//       left: None,
//       right: None
//     }
//   }
// }
use std::rc::Rc;
use std::cell::RefCell;
impl Solution {
  pub fn boundary_of_binary_tree(root: Option<Rc<RefCell<TreeNode>>>) -> Vec<i32> {
    let root = match root {
      Some(r) => r,
      None => return vec![],
    };
    let mut result = vec![root.borrow().val];
    if root.borrow().left.is_none() && root.borrow().right.is_none() {
      return result;
    }
    // Left boundary (excluding root and leaves)
    Self::left_boundary(&root.borrow().left, &mut result);
    // Leaves
    Self::leaves(&Some(root.clone()), &mut result);
    // Right boundary (excluding root and leaves, reversed)
    let mut right = vec![];
    Self::right_boundary(&root.borrow().right, &mut right);
    right.reverse();
    result.extend(right);
    result
  }
  
  fn left_boundary(node: &Option<Rc<RefCell<TreeNode>>>, result: &mut Vec<i32>) {
    if let Some(n) = node {
      let b = n.borrow();
      if b.left.is_none() && b.right.is_none() { return; }
      result.push(b.val);
      if b.left.is_some() {
        Self::left_boundary(&b.left, result);
      } else {
        Self::left_boundary(&b.right, result);
      }
    }
  }
  
  fn right_boundary(node: &Option<Rc<RefCell<TreeNode>>>, result: &mut Vec<i32>) {
    if let Some(n) = node {
      let b = n.borrow();
      if b.left.is_none() && b.right.is_none() { return; }
      result.push(b.val);
      if b.right.is_some() {
        Self::right_boundary(&b.right, result);
      } else {
        Self::right_boundary(&b.left, result);
      }
    }
  }
  
  fn leaves(node: &Option<Rc<RefCell<TreeNode>>>, result: &mut Vec<i32>) {
    if let Some(n) = node {
      let b = n.borrow();
      if b.left.is_none() && b.right.is_none() {
        result.push(b.val);
        return;
      }
      Self::leaves(&b.left, result);
      Self::leaves(&b.right, result);
    }
  }
}