最大树定义:一个树,其中每个节点的值都大于其子树中的任何其他值。
给出最大树的根节点 root。
就像之前的问题那样,给定的树是从列表 A(root = Construct(A))递归地使用下述 Construct(A) 例程构造的:
如果 A 为空,返回 null
否则,令 A[i] 作为 A 的最大元素。创建一个值为 A[i] 的根节点 root
root 的左子树将被构建为 Construct([A[0], A[1], …, A[i-1]])
root 的右子树将被构建为 Construct([A[i+1], A[i+2], …, A[A.length - 1]])
返回 root
请注意,我们没有直接给定 A,只有一个根节点 root = Construct(A).
假设 B 是 A 的副本,并在末尾附加值 val。题目数据保证 B 中的值是不同的。
返回 Construct(B)。
例:
输入:root = [4,1,3,null,null,2], val = 5
输出:[5,4,null,1,3,null,null,2]
解释:A = [1,4,2,3], B = [1,4,2,3,5]
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
struct TreeNode* Insert(struct TreeNode* root, struct TreeNode* pre, int val, struct TreeNode* head)
{
if(!pre && root->val < val){//头结点
struct TreeNode* p = (struct TreeNode* )malloc(sizeof(struct TreeNode));
p->val = val;
p->left = root;
p->right = NULL;
return p;
}
if(root->val > val){
if(root->right){
head = Insert(root->right, root, val, head);
return head;
}else{
struct TreeNode* p = (struct TreeNode*)malloc(sizeof(struct TreeNode));
p->val = val;
root->right = p;
p->right = NULL;
p->left = NULL;
return head;
}
}else{
struct TreeNode* p = (struct TreeNode* )malloc(sizeof(struct TreeNode));
p->val = val;
p->left = root;
pre->right = p;
p->right = NULL;
return head;
}
}
struct TreeNode* insertIntoMaxTree(struct TreeNode* root, int val){
if(!root) return NULL;
else{
struct TreeNode* head = root;
return Insert(root, NULL, val, head);
}
}