Boundary Traversal of a Binary Tree

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)

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Recovering from a Jupyter Disaster

What is a PWA?

Google Docs — How to Convert Image to Editable Text

How to Trace APIs with Jaeger-Opentracing?

Free Snow Leopard Dmg Download

How To Deposit Funds To The Minty Platform

Tokenarium: First steps and Hiring a team

What is a “C” library? What is it for?

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Abhinay Gupta

Abhinay Gupta

More from Medium

CRISIS IN MULTIVERSE

A weblog for Machine Learning : from scratch to advance level

Ghazal and Repetition

Pleasure reading session 4