File tree 1 file changed +41
-0
lines changed
1 file changed +41
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ class TreeNode {
3
+ int data ;
4
+ Node left , right ;
5
+ public TreeNode (int data ) {
6
+ this .data = data ;
7
+ left = right = null ;
8
+ }
9
+ }
10
+
11
+ public List <Integer > boundaryTraversal (TreeNode root ) {
12
+ List <Integer > res = new ArrayList <>();
13
+ getBoundary (root , res , -1 );
14
+ getBoundary (root , res , 0 );
15
+ getBoundary (root , res , 1 );
16
+
17
+ return res ;
18
+ }
19
+
20
+ public void getBoundary (TreeNode root , List <Integer > res , int dir ) {
21
+ if (root == null ) return ;
22
+
23
+ if (dir == -1 ) {
24
+ if (root .left != null ) {
25
+ res .add (root .val );
26
+ getBoundary (root .left , res , dir );
27
+ }
28
+ } else if (dir == 0 ) {
29
+ getBoundary (root .left , res , dir );
30
+ if (root .left == null && root .right == null ) {
31
+ res .add (root .val );
32
+ }
33
+ getBoundary (root .right , res , dir );
34
+ } else if (dir == 1 ) {
35
+ if (root .right != null ) {
36
+ res .add (root .val );
37
+ getBoundary (root .right , res , dir );
38
+ }
39
+ }
40
+ }
41
+ }
You can’t perform that action at this time.
0 commit comments