Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions src/main/java/g0201_0300/s0257_binary_tree_paths/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package g0201_0300.s0257_binary_tree_paths;

import com_github_leetcode.TreeNode;
import java.util.ArrayList;
import java.util.List;

/*
* 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;
* }
* }
*/
public class Solution {
private List<String> result;
private StringBuilder sb;

public List<String> binaryTreePaths(TreeNode root) {
result = new ArrayList<>();
if (root == null) {
return result;
}
sb = new StringBuilder();
walkThrough(root);
return result;
}

private void walkThrough(TreeNode root) {
assert root != null;
int length = sb.length();
sb.append(root.val);
length = sb.length() - length;
if (root.left == null && root.right == null) {
// leaf node.
result.add(sb.toString());
sb.delete(sb.length() - length, sb.length());
return;
}
sb.append("->");
length += 2;
if (root.left != null) {
walkThrough(root.left);
}
if (root.right != null) {
walkThrough(root.right);
}
sb.delete(sb.length() - length, sb.length());
}
}
26 changes: 26 additions & 0 deletions src/main/java/g0201_0300/s0257_binary_tree_paths/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
257\. Binary Tree Paths

Easy

Given the `root` of a binary tree, return _all root-to-leaf paths in **any order**_.

A **leaf** is a node with no children.

**Example 1:**

![](https://assets.leetcode.com/uploads/2021/03/12/paths-tree.jpg)

**Input:** root = \[1,2,3,null,5\]

**Output:** \["1->2->5","1->3"\]

**Example 2:**

**Input:** root = \[1\]

**Output:** \["1"\]

**Constraints:**

* The number of nodes in the tree is in the range `[1, 100]`.
* `-100 <= Node.val <= 100`
13 changes: 13 additions & 0 deletions src/main/java/g0201_0300/s0258_add_digits/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package g0201_0300.s0258_add_digits;

public class Solution {
public int addDigits(int num) {
if (num == 0) {
return 0;
}
if (num % 9 == 0) {
return 9;
}
return num % 9;
}
}
30 changes: 30 additions & 0 deletions src/main/java/g0201_0300/s0258_add_digits/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
258\. Add Digits

Easy

Given an integer `num`, repeatedly add all its digits until the result has only one digit, and return it.

**Example 1:**

**Input:** num = 38

**Output:** 2

**Explanation:**

The process is
38 --> 3 + 8 --> 11
11 --> 1 + 1 --> 2
Since 2 has only one digit, return it.

**Example 2:**

**Input:** num = 0

**Output:** 0

**Constraints:**

* <code>0 <= num <= 2<sup>31</sup> - 1</code>

**Follow up:** Could you do it without any loop/recursion in `O(1)` runtime?
21 changes: 21 additions & 0 deletions src/main/java/g0201_0300/s0260_single_number_iii/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package g0201_0300.s0260_single_number_iii;

public class Solution {
public int[] singleNumber(int[] nums) {
int xorSum = 0;
for (int num : nums) {
// will give xor of required nos
xorSum ^= num;
}
// find rightmost bit which is set
int rightBit = xorSum & -xorSum;
int a = 0;
for (int num : nums) {
// xor only those number whose rightmost bit is set
if ((num & rightBit) != 0) {
a ^= num;
}
}
return new int[] {a, a ^ xorSum};
}
}
31 changes: 31 additions & 0 deletions src/main/java/g0201_0300/s0260_single_number_iii/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
260\. Single Number III

Medium

Given an integer array `nums`, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only once. You can return the answer in **any order**.

You must write an algorithm that runs in linear runtime complexity and uses only constant extra space.

**Example 1:**

**Input:** nums = \[1,2,1,3,2,5\]

**Output:** \[3,5\] **Explanation: ** \[5, 3\] is also a valid answer.

**Example 2:**

**Input:** nums = \[-1,0\]

**Output:** \[-1,0\]

**Example 3:**

**Input:** nums = \[0,1\]

**Output:** \[1,0\]

**Constraints:**

* <code>2 <= nums.length <= 3 * 10<sup>4</sup></code>
* <code>-2<sup>31</sup> <= nums[i] <= 2<sup>31</sup> - 1</code>
* Each integer in `nums` will appear twice, only two integers will appear once.
25 changes: 25 additions & 0 deletions src/test/java/g0201_0300/s0257_binary_tree_paths/SolutionTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package g0201_0300.s0257_binary_tree_paths;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;

import com_github_leetcode.TreeNode;
import java.util.Arrays;
import org.junit.Test;

public class SolutionTest {
@Test
public void binaryTreePaths() {
TreeNode treeNode =
new TreeNode(1, new TreeNode(2, null, new TreeNode(5)), new TreeNode(3));
assertThat(
new Solution().binaryTreePaths(treeNode),
equalTo(Arrays.asList("1->2->5", "1->3")));
}

@Test
public void binaryTreePaths2() {
TreeNode treeNode = new TreeNode(1);
assertThat(new Solution().binaryTreePaths(treeNode), equalTo(Arrays.asList("1")));
}
}
18 changes: 18 additions & 0 deletions src/test/java/g0201_0300/s0258_add_digits/SolutionTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package g0201_0300.s0258_add_digits;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;

import org.junit.Test;

public class SolutionTest {
@Test
public void addDigits() {
assertThat(new Solution().addDigits(38), equalTo(2));
}

@Test
public void addDigits2() {
assertThat(new Solution().addDigits(0), equalTo(0));
}
}
20 changes: 20 additions & 0 deletions src/test/java/g0201_0300/s0260_single_number_iii/SolutionTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package g0201_0300.s0260_single_number_iii;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;

import org.junit.Test;

public class SolutionTest {
@Test
public void singleNumber() {
assertThat(
new Solution().singleNumber(new int[] {1, 2, 1, 3, 2, 5}),
equalTo(new int[] {3, 5}));
}

@Test
public void singleNumber2() {
assertThat(new Solution().singleNumber(new int[] {-1, 0}), equalTo(new int[] {-1, 0}));
}
}