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)