2654. Maximum Binary Tree

654. Maximum Binary Tree You are given an integer array nums with no duplicates. A maximum binary tree can be built recursively from nums using the following algorithm: Create a root node whose value is the maximum value in nums. Recursively build the left subtree on the subarray prefix to the left of the maximum value. Recursively build the right subtree on the subarray suffix to the right of the maximum value. Return the maximum binary tree built from nums. ...

September 3, 2021 · 3 min · volyx

307. Range Sum Query - Mutable

307. Range Sum Query - Mutable Given an integer array nums, handle multiple queries of the following types: Update the value of an element in nums. Calculate the sum of the elements of nums between indices left and right inclusive where left <= right. Implement the NumArray class: NumArray(int[] nums) Initializes the object with the integer array nums. void update(int index, int val) Updates the value of nums[index] to be val. int sumRange(int left, int right) Returns the sum of the elements of nums between indices left and right inclusive (i.e. nums[left] + nums[left + 1] + … + nums[right]). Example 1: Input ["NumArray", "sumRange", "update", "sumRange"] [[[1, 3, 5]], [0, 2], [1, 2], [0, 2]] Output [null, 9, null, 8] Explanation NumArray numArray = new NumArray([1, 3, 5]); numArray.sumRange(0, 2); // return 1 + 3 + 5 = 9 numArray.update(1, 2); // nums = [1, 2, 5] numArray.sumRange(0, 2); // return 1 + 2 + 5 = 8 Constraints: ...

September 3, 2021 · 3 min · volyx

270. Closest Binary Search Tree Value

270. Closest Binary Search Tree Value Given the root of a binary search tree and a target value, return the value in the BST that is closest to the target. Example 1: Input: root = [4,2,5,1,3], target = 3.714286 Output: 4 Example 2: Input: root = [1], target = 4.428571 Output: 1 Constraints: The number of nodes in the tree is in the range [1, 104]. 0 <= Node.val <= 10^9 -10^9 <= target <= 10^9 Solution /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; * this.left = left; * this.right = right; * } * } */ class Solution { //////////////// O(logN) //////////////////////// public int closestValue(TreeNode root, double target) { int closest = root.val; while (root != null) { if (Math.abs(root.val - target) < Math.abs(closest - target)) { closest = root.val; } root = root.val > target ? root.left: root.right; } return closest; } //////////////// O(N) //////////////////////// int closest; double closestDelta; public int closestValue2(TreeNode root, double target) { closest = root.val; closestDelta = Math.abs((double) root.val - target); find(root, target); return closest; } void find(TreeNode node, double target) { if (node == null) return; double currentDelta = Math.abs((double) node.val - target); if (currentDelta < closestDelta) { closest = node.val; closestDelta = currentDelta; } find(node.left, target); find(node.right, target); } ///////////// In Order Traversal O(N) public int closestValue1(TreeNode root, double target) { Deque<TreeNode> deq = new ArrayDeque<>(); long prev = Long.MIN_VALUE; while (deq.size() > 0 || root != null) { while (root != null) { deq.offerLast(root); root = root.left; } root = deq.removeLast(); if (prev <= target && target < root.val) { if (Math.abs(target - prev) < Math.abs(target - root.val)) { return (int) prev; } else { return root.val; } } prev = root.val; root = root.right; } return (int) prev; } } Solution 2022-01-30 class Solution { public int closestValue(TreeNode root, double target) { int closest = root.val; while (root != null) { if (Math.abs(target - root.val) < Math.abs(closest - target)) { closest = root.val; } root = (root.val > target) ? root.left: root.right; } return closest; } }

July 26, 2021 · 2 min · volyx

515. Find Largest Value in Each Tree Row

515. Find Largest Value in Each Tree Row Given the root of a binary tree, return an array of the largest value in each row of the tree (0-indexed). Example 1: Input: root = [1,3,2,5,3,null,9] Output: [1,3,9] Example 2: Input: root = [1,2,3] Output: [1,3] Example 3: Input: root = [1] Output: [1] Example 4: Input: root = [1,null,2] Output: [1,2] Example 5: Input: root = [] Output: [] ...

July 19, 2021 · 2 min · volyx

938. Range Sum of BST

938. Range Sum of BST Given the root node of a binary search tree and two integers low and high, return the sum of values of all nodes with a value in the inclusive range [low, high]. Example 1: Input: root = [10,5,15,3,7,null,18], low = 7, high = 15 Output: 32 Explanation: Nodes 7, 10, and 15 are in the range [7, 15]. 7 + 10 + 15 = 32. ...

May 25, 2021 · 2 min · volyx

98. Validate Binary Search Tree

98. Validate Binary Search Tree Given the root of a binary tree, determine if it is a valid binary search tree (BST). A valid BST is defined as follows: The left subtree of a node contains only nodes with keys less than the node’s key. The right subtree of a node contains only nodes with keys greater than the node’s key. Both the left and right subtrees must also be binary search trees. Example 1: Input: root = [2,1,3] Output: true ...

May 7, 2021 · 2 min · volyx

1379. Find a Corresponding Node of a Binary Tree in a Clone of That Tree

1379. Find a Corresponding Node of a Binary Tree in a Clone of That Tree Given two binary trees original and cloned and given a reference to a node target in the original tree. The cloned tree is a copy of the original tree. Return a reference to the same node in the cloned tree. Note that you are not allowed to change any of the two trees or the target node and the answer must be a reference to a node in the cloned tree. ...

April 6, 2021 · 2 min · volyx

538. Convert BST to Greater Tree

538. Convert BST to Greater Tree Given the root of a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original BST is changed to the original key plus sum of all keys greater than the original key in BST. As a reminder, a binary search tree is a tree that satisfies these constraints: The left subtree of a node contains only nodes with keys less than the node’s key. The right subtree of a node contains only nodes with keys greater than the node’s key. Both the left and right subtrees must also be binary search trees. Note: This question is the same as 1038: https://leetcode.com/problems/binary-search-tree-to-greater-sum-tree/ ...

April 6, 2021 · 2 min · volyx

897. Increasing Order Search Tree

897. Increasing Order Search Tree Given the root of a binary search tree, rearrange the tree in in-order so that the leftmost node in the tree is now the root of the tree, and every node has no left child and only one right child. Example 1: Input: root = [5,3,6,2,4,null,8,1,null,null,null,7,9] Output: [1,null,2,null,3,null,4,null,5,null,6,null,7,null,8,null,9] Example 2: Input: root = [5,1,7] Output: [1,null,5,null,7] Constraints: The number of nodes in the given tree will be in the range [1, 100]. 0 <= Node.val <= 1000 Solution /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; * this.left = left; * this.right = right; * } * } */ class Solution { TreeNode res = null; TreeNode next = null; public TreeNode increasingBST(TreeNode root) { if (root == null) return null; dfs(root); return res; } void dfs(TreeNode node) { if (node == null) return; dfs(node.left); if (res == null) { res = new TreeNode(node.val); next = res; } else { next.right = new TreeNode(node.val); next = next.right; } dfs(node.right); } }

April 6, 2021 · 1 min · volyx

1387. Sort Integers by The Power Value

1387. Sort Integers by The Power Value The power of an integer x is defined as the number of steps needed to transform x into 1 using the following steps: if x is even then x = x / 2 if x is odd then x = 3 * x + 1 For example, the power of x = 3 is 7 because 3 needs 7 steps to become 1 (3 –> 10 –> 5 –> 16 –> 8 –> 4 –> 2 –> 1). ...

April 3, 2021 · 3 min · volyx