Related: LintCode 126 - Max Tree
LeetCode 998 - Maximum Binary Tree II
LeetCode 654 - Maximum Binary Tree
https://leetcode.com/problems/maximum-binary-tree-ii/
LeetCode 998 - Maximum Binary Tree II
LeetCode 654 - Maximum Binary Tree
https://leetcode.com/problems/maximum-binary-tree-ii/
We are given the
root
node of a maximum tree: a tree where every node has a value greater than any other value in its subtree.
Just as in the previous problem, the given tree was constructed from an list
A
(root = Construct(A)
) recursively with the following Construct(A)
routine:- If
A
is empty, returnnull
. - Otherwise, let
A[i]
be the largest element ofA
. Create aroot
node with valueA[i]
. - The left child of
root
will beConstruct([A[0], A[1], ..., A[i-1]])
- The right child of
root
will beConstruct([A[i+1], A[i+2], ..., A[A.length - 1]])
- Return
root
.
Note that we were not given A directly, only a root node
root = Construct(A)
.
Suppose
B
is a copy of A
with the value val
appended to it. It is guaranteed that B
has unique values.
Return
Construct(B)
.
Example 1:
Input: root = [4,1,3,null,null,2], val = 5 Output: [5,4,null,1,3,null,null,2] Explanation: A = [1,4,2,3], B = [1,4,2,3,5]https://leetcode.com/problems/maximum-binary-tree-ii/discuss/242936/JavaC%2B%2BPython-Recursion-and-Iteration
If root.val > val, recusion on the right.
Else, put right subtree on the left of new node
Else, put right subtree on the left of new node
TreeNode(val)
public TreeNode insertIntoMaxTree(TreeNode root, int val) {
if (root != null && root.val > val) {
root.right = insertIntoMaxTree(root.right, val);
return root;
}
TreeNode node = new TreeNode(val);
node.left = root;
return node;
}
https://leetcode.com/problems/maximum-binary-tree-ii/discuss/242897/Java-clean-recursive-solution
The idea is to insert node to the right parent or right sub-tree of current node. Using recursion can achieve this:
If inserted value is greater than current node, the inserted goes to right parent
If inserted value is smaller than current node, we recursively re-cauculate right subtree
If inserted value is greater than current node, the inserted goes to right parent
If inserted value is smaller than current node, we recursively re-cauculate right subtree
public TreeNode insertIntoMaxTree(TreeNode root, int v) {
if(root==null)return new TreeNode(v);
if(root.val<v){
TreeNode node = new TreeNode(v);
node.left=root;
return node;
}
root.right=insertIntoMaxTree(root.right,v);
return root;
}
https://leetcode.com/problems/maximum-binary-tree-ii/discuss/243188/How-many-people-can't-understand-what-the-question-means我解释下为什么当val<root.val时,是往右边加的。
1. the given tree was constructed from an list A (root = Construct(A)). So, List<Integer> A = new ArrayList();
2. Suppose B is a copy of A with the value val appended to it. So, B = new ArrayList(A) and B.add(val);
3. The left child of root will be Construct([A[0], A[1], ..., A[i-1]]),
The right child of root will be Construct([A[i+1], A[i+2], ..., A[A.length - 1]]).
in this case A represent B, B[B.length-1] = val, So.
4. If val is the largest, i = B.length-1, the root node's value is val, i=0 to i-1 are in the left child of root.
This explains why when val > root.val, root should be the left child of new node with value val.
5. Else val is not the largest, the new node with value val is always the right child of root.
X. Solution 2: Iteration
Search on the right, find the node that
Then create new node
put old
put
cur.val > val > cur.right.val
Then create new node
TreeNode(val)
,put old
cur.right
as node.left
,put
node
as new cur.right
. public TreeNode insertIntoMaxTree(TreeNode root, int val) {
TreeNode node = new TreeNode(val), cur = root;
if (root.val < val) {
node.left = root;
return node;
}
while (cur.right != null && cur.right.val > val) {
cur = cur.right;
}
node.left = cur.right;
cur.right = node;
return root;
}