Skip to content

Commit

Permalink
feat: add solutions to lc problem: No.0998.Maximum Binary Tree II
Browse files Browse the repository at this point in the history
  • Loading branch information
yanglbme committed Aug 6, 2021
1 parent c8fea0e commit f1240f5
Show file tree
Hide file tree
Showing 6 changed files with 252 additions and 4 deletions.
93 changes: 91 additions & 2 deletions solution/0900-0999/0998.Maximum Binary Tree II/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,27 +68,116 @@

<p> </p>


## 解法

<!-- 这里可写通用的实现逻辑 -->

已知最大树 A,插入一个值 val 后,返回插入后的树。

如果 val 是最大数,那么将 val 作为新的根节点,root 作为新的根节点的左子树。

如果 val 不是最大数,由于 val 是在最后追加的数,那么一定是在 root 的右边,所以将 val 作为新节点插入 root 的右子树即可。

<!-- tabs:start -->

### **Python3**

<!-- 这里可写当前语言的特殊实现逻辑 -->

```python

# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def insertIntoMaxTree(self, root: TreeNode, val: int) -> TreeNode:
if root is None or root.val < val:
return TreeNode(val, root, None)
root.right = self.insertIntoMaxTree(root.right, val)
return root
```

### **Java**

<!-- 这里可写当前语言的特殊实现逻辑 -->

```java
/**
* 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 {
public TreeNode insertIntoMaxTree(TreeNode root, int val) {
if (root == null || root.val < val) {
return new TreeNode(val, root, null);
}
root.right = insertIntoMaxTree(root.right, val);
return root;
}
}
```

### **C++**

```cpp
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
TreeNode* insertIntoMaxTree(TreeNode* root, int val) {
if (root == nullptr || root->val < val) {
return new TreeNode(val, root, nullptr);
}
root->right = insertIntoMaxTree(root->right, val);
return root;
}
};
```
### **Go**
```go
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func insertIntoMaxTree(root *TreeNode, val int) *TreeNode {
if root == nil || root.Val < val {
return &TreeNode{
Val: val,
Left: root,
Right: nil,
}
}
root.Right = insertIntoMaxTree(root.Right, val)
return root
}
```

### **...**
Expand Down
87 changes: 85 additions & 2 deletions solution/0900-0999/0998.Maximum Binary Tree II/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,21 +60,104 @@
<li><code>1 &lt;= B.length &lt;= 100</code></li>
</ul>


## Solutions

<!-- tabs:start -->

### **Python3**

```python

# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def insertIntoMaxTree(self, root: TreeNode, val: int) -> TreeNode:
if root is None or root.val < val:
return TreeNode(val, root, None)
root.right = self.insertIntoMaxTree(root.right, val)
return root
```

### **Java**

```java
/**
* 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 {
public TreeNode insertIntoMaxTree(TreeNode root, int val) {
if (root == null || root.val < val) {
return new TreeNode(val, root, null);
}
root.right = insertIntoMaxTree(root.right, val);
return root;
}
}
```

### **C++**

```cpp
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
TreeNode* insertIntoMaxTree(TreeNode* root, int val) {
if (root == nullptr || root->val < val) {
return new TreeNode(val, root, nullptr);
}
root->right = insertIntoMaxTree(root->right, val);
return root;
}
};
```
### **Go**
```go
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func insertIntoMaxTree(root *TreeNode, val int) *TreeNode {
if root == nil || root.Val < val {
return &TreeNode{
Val: val,
Left: root,
Right: nil,
}
}
root.Right = insertIntoMaxTree(root.Right, val)
return root
}
```

### **...**
Expand Down
21 changes: 21 additions & 0 deletions solution/0900-0999/0998.Maximum Binary Tree II/Solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
TreeNode* insertIntoMaxTree(TreeNode* root, int val) {
if (root == nullptr || root->val < val) {
return new TreeNode(val, root, nullptr);
}
root->right = insertIntoMaxTree(root->right, val);
return root;
}
};
19 changes: 19 additions & 0 deletions solution/0900-0999/0998.Maximum Binary Tree II/Solution.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func insertIntoMaxTree(root *TreeNode, val int) *TreeNode {
if root == nil || root.Val < val {
return &TreeNode{
Val: val,
Left: root,
Right: nil,
}
}
root.Right = insertIntoMaxTree(root.Right, val)
return root
}
24 changes: 24 additions & 0 deletions solution/0900-0999/0998.Maximum Binary Tree II/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* 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 {
public TreeNode insertIntoMaxTree(TreeNode root, int val) {
if (root == null || root.val < val) {
return new TreeNode(val, root, null);
}
root.right = insertIntoMaxTree(root.right, val);
return root;
}
}
12 changes: 12 additions & 0 deletions solution/0900-0999/0998.Maximum Binary Tree II/Solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def insertIntoMaxTree(self, root: TreeNode, val: int) -> TreeNode:
if root is None or root.val < val:
return TreeNode(val, root, None)
root.right = self.insertIntoMaxTree(root.right, val)
return root

0 comments on commit f1240f5

Please sign in to comment.