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

What is NEXT.Chain- The Next DeFi Powerhouse

KYVE Incentivized Testnet

Linux Network Namespace Usage

Life is a journey of twists and turns, peaks and valleys, mountains to climb and oceans to explore.

Visualizing Graphs in 3D with WebGL

Using Rsyslog to ship the logs over TCP on CentOS.

Custom Fonts in Xamarin.Forms app

Weekly State of The Pad: Month of February 2022

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

Physics — Introduction to Current Electricity

Chapter Two of Old Paper

On the Run: 2 Peat