Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 7015aa4

Browse files
committed
updates
1 parent cfd10ba commit 7015aa4

File tree

4 files changed

+46
-0
lines changed

4 files changed

+46
-0
lines changed

‎100_same_tree.scala‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
object Solution {
2+
def isSameTree(p: TreeNode, q: TreeNode): Boolean = {
3+
if (p == null && q == null) true
4+
else if (p == null && q != null || p != null && q == null) false
5+
else if (p.value == q.value)
6+
isSameTree(p.left, q.left) && isSameTree(p.right, q.right)
7+
else
8+
false
9+
}
10+
}

‎110_balanced_binary_tree.scala‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
object Solution {
2+
3+
def balancedAndDepth(root: TreeNode): (Boolean, Int) = {
4+
if (root == null) (true, 0)
5+
else{
6+
val resLeft = balancedAndDepth(root.left)
7+
val resRight = balancedAndDepth(root.right)
8+
(resLeft._1 && resRight._1 && (Math.abs(resLeft._2 - resRight._2) <= 1), Math.max(resLeft._2 + 1, resRight._2 + 1))
9+
}
10+
}
11+
12+
def isBalanced(root: TreeNode): Boolean = {
13+
balancedAndDepth(root)._1
14+
}
15+
}

‎112_path_sum.scala‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
object Solution {
2+
def hasPathSum(root: TreeNode, sum: Int): Boolean = {
3+
if (root == null) false
4+
else if (root.left == null && root.right == null) sum == root.value
5+
else hasPathSum(root.left, sum - root.value) || hasPathSum(root.right, sum - root.value)
6+
}
7+
}

‎617_merge_two_binary_trees.scala‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
object Solution {
2+
3+
def mergeTrees(t1: TreeNode, t2: TreeNode): TreeNode = {
4+
if (t1 == null && t2 == null) null
5+
else if (t1 == null) t2
6+
else if (t2 == null) t1
7+
else{
8+
t1.value += t2.value
9+
t1.left = mergeTrees(t1.left, t2.left)
10+
t1.right = mergeTrees(t1.right, t2.right)
11+
t1
12+
}
13+
}
14+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /