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
class Solution { | |
public int[] twoSum(int[] nums, int target) { | |
Map<Integer, Integer> map = new HashMap<>(); // to store diff, position as key-value pair | |
int[] ans = new int[2]; | |
for(int i = 0; i< nums.length; i++) { | |
if(map.containsKey(nums[i])) { | |
ans[0] = map.get(nums[i]); | |
ans[1] = i; | |
return ans; | |
} else { |
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
class Solution { | |
public int rob(int[] nums) { | |
int n = nums.length; | |
int[] dp = new int[n]; | |
dp[0] = nums[0]; | |
for(int i = 1; i< n;i++) { | |
int pick = nums[i]; | |
if(i>1) pick += dp[i-2]; | |
int nonPick = dp[i-1]; | |
dp[i] = Math.max(pick, nonPick); |
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
class Solution { | |
public int rob(int[] nums) { | |
int n = nums.length; | |
int[] dp = new int[n+1]; | |
Arrays.fill(dp,-1); | |
return rec(n-1, nums, dp); | |
} | |
private int rec(int i, int[] nums, int[] dp) { | |
if(i == 0) return nums[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
import java.util.ArrayDeque | |
import java.util.LinkedList | |
fun postOrderIterative(root: TreeNode?): List<Int> { | |
if(root == null) return emptyList() | |
val stack = ArrayDeque<TreeNode>() | |
val list = LinkedList<Int>() | |
stack.push(root) | |
while (stack.isNotEmpty()) { | |
val node = stack.pop() |
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
fun postOrderRecursive(root: TreeNode?) : List<Int>{ | |
if(root == null) return emptyList() | |
return postOrderRecursive(root.left) + postOrderRecursive(root.right) + listOf(root.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
import java.util.ArrayDeque | |
fun inorderTraversalIterative(root: TreeNode?): List<Int> { | |
val list = mutableListOf<Int>() | |
if (root == null) return list | |
var node = root | |
val stack = ArrayDeque<TreeNode>() | |
// traversing the tree whenever right node is not null or the stack contains items | |
while (node != null || stack.isNotEmpty()) { | |
// processing all the left nodes of the current node |
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
fun inOrderTraversalRecursive(root: TreeNode?) : List<Int> { | |
if (root == null) return emptyList() | |
return inOrderTraversalRecursive(root.left) + listOf(root.data) + inOrderTraversalRecursive(root.right) | |
} |
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
import java.util.ArrayDeque | |
fun preOrderTraversalIterative(root: TreeNode?): List<Int> { | |
val myList = mutableListOf<Int>() | |
// creating stack to store the left and right nodes while processing root node | |
val stack = ArrayDeque<TreeNode>() | |
// checking edge case and returning empty list | |
if (root == null) return myList | |
var node = root | |
while (node != null || stack.isNotEmpty()) { |
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
class TreeNode(var data: Int, var left: TreeNode? = null, var right: TreeNode? = null) | |
fun initializeBinaryTree(): TreeNode { | |
val c = TreeNode(4) | |
val d = TreeNode(5) | |
val a = TreeNode(2, c, d) | |
val b = TreeNode(3) | |
return TreeNode(1, a, b) | |
} |
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
fun preorderTraversal(root: TreeNode?): List<Int> { | |
// if root is null, return list of empty array | |
if (root == null) { | |
return listOf() | |
} | |
// iterate recursively into left child and right child | |
return listOf(root.data) + preorderTraversal(root.left) + preorderTraversal(root.right) | |
} |