-
-
Notifications
You must be signed in to change notification settings - Fork 7.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add solutions to lc problem: No.0998.Maximum Binary Tree II
- Loading branch information
Showing
6 changed files
with
252 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
solution/0900-0999/0998.Maximum Binary Tree II/Solution.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
19
solution/0900-0999/0998.Maximum Binary Tree II/Solution.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
24
solution/0900-0999/0998.Maximum Binary Tree II/Solution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
12
solution/0900-0999/0998.Maximum Binary Tree II/Solution.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |