-
-
Notifications
You must be signed in to change notification settings - Fork 300
/
998.java
55 lines (55 loc) · 1.59 KB
/
998.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
__________________________________________________________________________________________________
sample 0 ms submission
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public TreeNode insertIntoMaxTree(TreeNode root, int val) {
return helper(root, val);
}
private TreeNode helper(TreeNode root, int val){
TreeNode current = new TreeNode(val);
if(root == null) return current;
if(root.val < val){
current.left = root;
return current;
}
else{
root.right = helper(root.right, val);
return root;
}
}
}
__________________________________________________________________________________________________
sample 36548 kb submission
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public TreeNode insertIntoMaxTree(TreeNode root, int val) {
if (root == null)
return new TreeNode(val);
if (val > root.val) {
TreeNode newNode = new TreeNode(val);
newNode.left = root;
return newNode;
} else {
root.right = insertIntoMaxTree(root.right, val);
// root.left = insertIntoMaxTree(root.left, val);
}
return root;
}
}
__________________________________________________________________________________________________