Boundary Traversal of a Binary Tree

Abhinay Gupta
Jul 6, 2021

--

Understanding Boundary Traversal


class Solution {
List<Integer> list;
public List<Integer> boundaryOfBinaryTree(TreeNode root) {
list= new ArrayList();
list.add(root.val);
leftview(root.left);
leaves(root.left);
leaves(root.right);
rightview(root.right);
return list;
}
public void leftview(TreeNode root){
if(root==null) return;
if(root.left==null&&root.right==null) return;
this.list.add(root.val);
if(root.left==null) leftview(root.right);
else leftview(root.left);
}
public void leaves(TreeNode root){
if(root==null) return;
if(root.left==null&&root.right==null) this.list.add(root.val);
leaves(root.left);
leaves(root.right);
}
public void rightview(TreeNode root){
if(root==null) return;
if(root.left==null&&root.right==null) return;
if(root.right==null) rightview(root.left);
else rightview(root.right);
this.list.add(root.val);
}
}

Time Complexity : O(n)

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Abhinay Gupta
Abhinay Gupta

Written by Abhinay Gupta

Exploring personal growth, tech, and culture through engaging storytelling | Inspiring and connecting with readers worldwide.

No responses yet

Write a response