Skip to content

Commit 872e1b1

Browse files
committed
Added 1 solution & modified 3 solutions
1 parent 91c7533 commit 872e1b1

4 files changed

+88
-43
lines changed

Easy/Convert BST to greater tree.java

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,19 @@
88
* }
99
*/
1010
class Solution {
11-
12-
int sum = 0;
13-
public TreeNode convertBST(TreeNode root) {
14-
convert(root);
15-
return root;
16-
}
17-
18-
public void convert(TreeNode cur) {
19-
if (cur == null) return;
20-
convert(cur.right);
21-
cur.val += sum;
22-
sum = cur.val;
23-
convert(cur.left);
11+
int updatedVal = 0;
12+
public TreeNode convertBST(TreeNode root) {
13+
helper(root);
14+
return root;
15+
}
16+
17+
private void helper(TreeNode root) {
18+
if (root == null) {
19+
return;
2420
}
21+
helper(root.right);
22+
updatedVal += root.val;
23+
root.val = updatedVal;
24+
helper(root.left);
25+
}
2526
}

Easy/Merge two binary trees.java

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,20 @@
88
* }
99
*/
1010
class Solution {
11-
public TreeNode mergeTrees(TreeNode t1, TreeNode t2) {
12-
if (t1 == null && t2 == null) {
13-
return null;
14-
}
15-
16-
int v = (t1 == null ? 0 : t1.val) + (t2 == null ? 0 : t2.val);
17-
TreeNode newNode = new TreeNode(v);
18-
19-
newNode.left = mergeTrees(t1==null ? null : t1.left, t2==null ? null : t2.left);
20-
newNode.right = mergeTrees(t1==null ? null : t1.right, t2==null ? null : t2.right);
21-
22-
return newNode;
11+
public TreeNode mergeTrees(TreeNode t1, TreeNode t2) {
12+
if (t1 == null && t2 == null) {
13+
return null;
2314
}
15+
int val = (t1 == null ? 0 : t1.val) + (t2 == null ? 0 : t2.val);
16+
TreeNode root = new TreeNode(val);
17+
root.left = mergeTrees(
18+
t1 == null ? null : t1.left,
19+
t2 == null ? null : t2.left
20+
);
21+
root.right = mergeTrees(
22+
t1 == null ? null : t1.right,
23+
t2 == null ? null : t2.right
24+
);
25+
return root;
26+
}
2427
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode(int x) { val = x; }
8+
* }
9+
*/
10+
class Solution {
11+
List<String> list;
12+
public int sumRootToLeaf(TreeNode root) {
13+
list = new ArrayList<>();
14+
helper(root, new StringBuilder());
15+
int ans = 0;
16+
for (String num : list) {
17+
ans += getIntegerVal(num);
18+
}
19+
return ans;
20+
}
21+
22+
private void helper(TreeNode root, StringBuilder sb) {
23+
if (root == null) {
24+
return;
25+
}
26+
sb.append(root.val);
27+
if (root.left == null && root.right == null) {
28+
list.add(sb.toString());
29+
}
30+
else {
31+
helper(root.left, new StringBuilder(sb.toString()));
32+
helper(root.right, new StringBuilder(sb.toString()));
33+
}
34+
}
35+
36+
private int getIntegerVal(String s) {
37+
int num = 0;
38+
int pow = 1;
39+
for (int i = s.length() - 1; i >= 0; i--) {
40+
num += Character.getNumericValue(s.charAt(i)) * pow;
41+
pow *= 2;
42+
}
43+
return num;
44+
}
45+
}

Easy/Univalued Binary Tree.java

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,20 @@
88
* }
99
*/
1010
class Solution {
11-
int currVal;
12-
public boolean isUnivalTree(TreeNode root) {
13-
currVal = Integer.MAX_VALUE;
14-
return isUnivalTreeHelper(root);
11+
public boolean isUnivalTree(TreeNode root) {
12+
if (root == null) {
13+
return true;
1514
}
16-
17-
private boolean isUnivalTreeHelper(TreeNode root) {
18-
if (root == null) {
19-
return true;
20-
}
21-
22-
if (currVal == Integer.MAX_VALUE) {
23-
currVal = root.val;
24-
}
25-
else if (currVal != root.val) {
26-
return false;
27-
}
28-
29-
return isUnivalTreeHelper(root.left) && isUnivalTreeHelper(root.right);
15+
return helper(root, root.val);
16+
}
17+
18+
private boolean helper(TreeNode root, int val) {
19+
if (root == null) {
20+
return true;
3021
}
22+
if (root.val != val) {
23+
return false;
24+
}
25+
return helper(root.left, val) && helper(root.right, val);
26+
}
3127
}

0 commit comments

Comments
 (0)