|
| 1 | +import java.util.*; |
| 2 | + |
| 3 | +/** |
| 4 | + * Created by gouthamvidyapradhan on 27/03/2017. |
| 5 | + * Accepted |
| 6 | + */ |
| 7 | +public class BoundaryOfBinaryTree |
| 8 | +{ |
| 9 | + public static class TreeNode { |
| 10 | + int val; |
| 11 | + TreeNode left; |
| 12 | + TreeNode right; |
| 13 | + TreeNode(int x) { val = x; } |
| 14 | + } |
| 15 | + |
| 16 | + private Set<TreeNode> done = new HashSet<>(); |
| 17 | + /** |
| 18 | + * Main method |
| 19 | + * @param args |
| 20 | + * @throws Exception |
| 21 | + */ |
| 22 | + public static void main(String[] args) throws Exception |
| 23 | + { |
| 24 | + TreeNode root = new TreeNode(1); |
| 25 | + /*root.right = new TreeNode(2); |
| 26 | + root.right.right = new TreeNode(3); |
| 27 | + root.right.right.right = new TreeNode(4); |
| 28 | + root.right.right.right.left = new TreeNode(5); |
| 29 | + root.right.right.right.left.right = new TreeNode(6); |
| 30 | + /*root.left = new TreeNode(2); |
| 31 | + root.left.left = new TreeNode(4); |
| 32 | + root.left.right = new TreeNode(5); |
| 33 | + root.left.right.left = new TreeNode(7); |
| 34 | + root.left.right.right = new TreeNode(8); |
| 35 | + root.right = new TreeNode(3); |
| 36 | + root.right.left = new TreeNode(6); |
| 37 | + root.right.left.left = new TreeNode(9); |
| 38 | + root.right.left.right = new TreeNode(10);*/ |
| 39 | + System.out.println(new BoundaryOfBinaryTree().boundaryOfBinaryTree(root)); |
| 40 | + } |
| 41 | + |
| 42 | + public List<Integer> boundaryOfBinaryTree(TreeNode root) |
| 43 | + { |
| 44 | + if(root == null) return new ArrayList<>(); |
| 45 | + List<Integer> antiClockwiseCollection = new ArrayList<>(); |
| 46 | + List<Integer> collection = new ArrayList<>(); |
| 47 | + |
| 48 | + if(root.left != null) |
| 49 | + leftShoulder(root, collection); |
| 50 | + else |
| 51 | + { |
| 52 | + if(!done.contains(root)) |
| 53 | + { |
| 54 | + done.add(root); |
| 55 | + collection.add(root.val); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + antiClockwiseCollection.addAll(collection); |
| 60 | + collection.clear(); |
| 61 | + leafNode(root, collection); |
| 62 | + antiClockwiseCollection.addAll(collection); |
| 63 | + collection.clear(); |
| 64 | + |
| 65 | + if(root.right != null) |
| 66 | + rightShoulder(root, collection); |
| 67 | + else |
| 68 | + { |
| 69 | + if(!done.contains(root)) |
| 70 | + { |
| 71 | + done.add(root); |
| 72 | + collection.add(root.val); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + Stack<Integer> stack = new Stack<>(); |
| 77 | + stack.addAll(collection); |
| 78 | + while(!stack.isEmpty()) |
| 79 | + antiClockwiseCollection.add(stack.pop()); |
| 80 | + return new ArrayList<>(antiClockwiseCollection); |
| 81 | + } |
| 82 | + |
| 83 | + private void leftShoulder(TreeNode node, List<Integer> list) |
| 84 | + { |
| 85 | + if(node == null) return; |
| 86 | + if(!done.contains(node)) |
| 87 | + { |
| 88 | + list.add(node.val); |
| 89 | + done.add(node); |
| 90 | + } |
| 91 | + |
| 92 | + if(node.left != null) leftShoulder(node.left, list); |
| 93 | + else if(node.right != null) leftShoulder(node.right, list); |
| 94 | + } |
| 95 | + |
| 96 | + private void rightShoulder(TreeNode node, List<Integer> list) |
| 97 | + { |
| 98 | + if(node == null) return; |
| 99 | + if(!done.contains(node)) |
| 100 | + { |
| 101 | + list.add(node.val); |
| 102 | + done.add(node); |
| 103 | + } |
| 104 | + if(node.right != null) rightShoulder(node.right, list); |
| 105 | + else if(node.left != null) rightShoulder(node.left, list); |
| 106 | + } |
| 107 | + |
| 108 | + private void leafNode(TreeNode node, List<Integer> list) |
| 109 | + { |
| 110 | + if(node == null) return; |
| 111 | + if(node.left == null && node.right == null) |
| 112 | + if(!done.contains(node)) |
| 113 | + { |
| 114 | + list.add(node.val); |
| 115 | + done.add(node); |
| 116 | + } |
| 117 | + leafNode(node.left, list); |
| 118 | + leafNode(node.right, list); |
| 119 | + } |
| 120 | +} |
0 commit comments