998. Maximum Binary Tree II(最大二叉树II)

本文探讨了在最大二叉树中插入新节点的两种方法:迭代和递归。详细解析了为何新节点总是在右侧插入,以及如何在保持树特性的同时找到正确的位置。通过代码示例展示了具体实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目描述

在这里插入图片描述在这里插入图片描述在这里插入图片描述

方法思路

Q1:Why to the right and not to the left?
Always go right since new element will be inserted at the end of the list.
Q2:why if(root.val<v){
TreeNode node = new TreeNode(v);
node.left=root;
return node;
},rather than if(root.val<v){
TreeNode node = new TreeNode(v);
node.right=root;
return node;
};
Is it just follow the example?

Approach1: iterative
Search on the right, find the node that 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;
    }

Approach2:recursive
这道题目描述的不是很清楚感觉上。(tricky!)
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

class Solution {
	//Runtime: 2 ms, faster than 100.00%
    //Memory Usage: 37 MB, less than 100.00% 
    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;
    }
}
### 最大二叉树的定义 最大二叉树是一种特殊的二叉树结构,其特点是根节点的值是数组中的最大值,而左子树和右子树分别由原数组中位于最大值左侧的部分和右侧的部分递归构建而成[^1]。 具体来说,给定一个不重复的整数数组 `nums`,可以按照以下方式构造一棵最大二叉树: - 数组的最大值作为根节点的数据。 - 左子树通过递归调用函数,在最大值左边的子数组上构建。 - 右子树通过递归调用函数,在最大值右边的子数组上构建。 这种构造方法确保了每棵子树都满足相同性质:即每个节点都是当前子数组中的最大值。 --- ### 实现方法 以下是基于 C++ 的最大二叉树实现代码示例。该代码利用递归来完成树的构建过程。 #### 定义节点结构体 ```cpp struct TreeNode { int val; TreeNode* left; TreeNode* right; TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} }; ``` #### 构建最大二叉树的核心逻辑 ```cpp TreeNode* constructMaximumBinaryTree(const std::vector<int>& nums) { if (nums.empty()) return nullptr; // 找到数组中的最大值及其位置 auto maxIt = std::max_element(nums.begin(), nums.end()); int maxValue = *maxIt; size_t maxIndex = std::distance(nums.begin(), maxIt); // 创建当前节点 TreeNode* root = new TreeNode(maxValue); // 递归构建左右子树 std::vector<int> leftSubarray(nums.begin(), nums.begin() + maxIndex); std::vector<int> rightSubarray(nums.begin() + maxIndex + 1, nums.end()); root->left = constructMaximumBinaryTree(leftSubarray); root->right = constructMaximumBinaryTree(rightSubarray); return root; } ``` #### 主程序测试 ```cpp #include <iostream> #include <vector> using namespace std; int main() { vector<int> nums = {3, 2, 1, 6, 0, 5}; TreeNode* root = constructMaximumBinaryTree(nums); // 输出结果验证(可扩展为层次遍历或其他形式) cout << "Root value of the maximum binary tree is: " << root->val << endl; return 0; } ``` --- ### 关键点解析 1. **寻找最大值** 使用标准库函数 `std::max_element` 来快速定位数组中的最大值以及对应的索引位置[^2]。 2. **递归分治法** 将问题分解成更小规模的子问题来解决。对于每一个子数组,找到其中的最大值并以此为中心划分左右两部分继续处理。 3. **边界条件** 当输入数组为空时返回空指针 (`nullptr`) 表示终止递归操作。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值