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
| public boolean canJump(int[] nums) { | |
| int len = nums.length - 1; | |
| int reachableIndex = len; | |
| for (int i = len; i >= 0; --i) { | |
| if (i + nums[i] >= reachableIndex) { | |
| reachableIndex = i; | |
| } | |
| } | |
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
| private int sum_tree_data(Node node){ | |
| int x, y; | |
| if (node != null){ | |
| x = sum_tree_data(node.left); | |
| y = sum_tree_data(node.right); | |
| return x + y + node.data; | |
| } | |
| return 0; | |
| } |
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
| private int treeHeight(Node node){ | |
| int x, y; | |
| if (node != null){ | |
| x = treeHeight(node.left); | |
| y = treeHeight(node.right); | |
| return (x > y) ? x + 1 : y + 1; | |
| } | |
| return 0; | |
| } |
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
| private int count_who_have_both_children(Node node){ | |
| int x, y; | |
| if (node != null){ | |
| x = count_who_have_both_children(node.left); | |
| y = count_who_have_both_children(node.right); | |
| if (node.left != null && node.right != null){ | |
| return x + y + 1; | |
| }else { | |
| return x + y; | |
| } |
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
| private int count(Node node){ | |
| int x, y; | |
| if (node != null){ | |
| x = count(node.left); | |
| y = count(node.right); | |
| return x + y +1; | |
| } | |
| return 0; | |
| } |
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
| private void levelOrder(Node node){ | |
| // 1. print the root data | |
| // 2. insert root address in queue | |
| // 3. while queue is not empty | |
| // a. get the address of the queue | |
| // check left child has data | |
| // i. print the left child data | |
| // j. insert data of left child | |
| // else check right child has data | |
| // i. print the right child data |
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
| private void iterativeInOrder(Node node){ | |
| // we are going to the left most item | |
| if (node == null) return; | |
| // create an empty stack and push root to it. | |
| Stack<Node> stack = new Stack<>(); | |
| while (node != null || !stack.isEmpty()){ | |
| // 1. inOrder(left_child) // put the data into stack | |
| // 2. Print(node.data) | |
| // 3. inOrder(right_child) |
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
| private void iterativePreOrder(Node node) { | |
| if (node == null) return; | |
| // create an empty stack and push root to it | |
| Stack<Node> stack = new Stack<>(); | |
| while (node != null || !stack.isEmpty()){ | |
| if (node != null){ | |
| System.out.print(node.data + " "); | |
| stack.push(node); | |
| node = node.left; |
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
| public int findKthLargestNumber(int[] arr, int k) { | |
| Queue<Integer> queue = new PriorityQueue<>((n1, n2) -> n1.compareTo(n2)); | |
| for(int n : arr){ | |
| queue.add(n); | |
| if (queue.size() > k){ | |
| queue.poll(); | |
| } | |
| } | |
| return queue.poll(); | |
| } |
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
| /** | |
| * Calculate two value sum if the list is unsorted | |
| * ========================================================== | |
| * @param arr | |
| * @param target | |
| */ | |
| public List<List<Integer>> sum_Three_HashMap_unSorted(int[] arr, int target){ | |
| Set<List<Integer>> result = new HashSet<>(); | |
| Set<Integer> dups = new HashSet<>(); | |
| Map<Integer, Integer> visited = new HashMap<>(); |