Created
February 3, 2019 12:15
-
-
Save summit87/6ea17233622a6298648f1ee2f6c6be89 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.algo.ds.practice.TreePractice; | |
| import com.algo.ds.practice.TreeNode; | |
| import java.util.HashSet; | |
| import java.util.Set; | |
| public class VerticalWidth { | |
| public static void main(String[] args) { | |
| TreeNode tn = new TreeNode(1); | |
| tn.setLeft(new TreeNode(2)); | |
| tn.setRight(new TreeNode(3)); | |
| tn.getRight().setLeft(new TreeNode(6)); | |
| tn.getRight().setRight(new TreeNode(7)); | |
| tn.getRight().getRight().setRight(new TreeNode(9)); | |
| tn.getRight().setLeft(new TreeNode(6)); | |
| tn.getRight().getLeft().setRight(new TreeNode(8)); | |
| tn.getLeft().setRight(new TreeNode(5)); | |
| tn.getLeft().setRight(new TreeNode(4)); | |
| Set<Integer> set = new HashSet<>(); | |
| maxVerticalWidth(tn,set,0); | |
| System.out.println("tn = " + set.size()); | |
| } | |
| private static void maxVerticalWidth(TreeNode tn, Set<Integer> set,int d){ | |
| if(tn == null){ | |
| return; | |
| } | |
| maxVerticalWidth(tn.getLeft(),set,d-1); | |
| set.add(d); | |
| maxVerticalWidth(tn.getRight(),set,d+1); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment