https://leetcode.com/problems/maximum-binary-tree-ii/

在654. Maximum Binary Tree版本的建树基础上,在最后插入一个数。

新节点要么为根节点,原树为其左子树;要么将新节点插在右子树中。

class Solution
{
public:
TreeNode* insertIntoMaxTree(TreeNode* root, int val)
{
if(root == NULL)
{
root = new TreeNode(val);
return root;
}
if(val > root->val)
{
TreeNode* temporary = root;
root = new TreeNode(val);
root->left = temporary;
return root;
}
else
{
root->right = insertIntoMaxTree(root->right, val);
return root;
}
}
};

leetcode_998. Maximum Binary Tree II的更多相关文章

  1. 【leetcode】998. Maximum Binary Tree II

    题目如下: We are given the root node of a maximum tree: a tree where every node has a value greater than ...

  2. [Swift]LeetCode998. 最大二叉树 II | 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 o ...

  3. 【LeetCode】998. Maximum Binary Tree II 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcod ...

  4. 654. Maximum Binary Tree

    654. Maximum Binary Tree 题目大意: 意思就是给你一组数,先选一个最大的作为根,这个数左边的数组作为左子树,右边的数组作为右子树,重复上一步. 读完就知道是递归了. 这个题真尼 ...

  5. [Leetcode Week14]Maximum Binary Tree

    Maximum Binary Tree 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/maximum-binary-tree/description/ ...

  6. Leetcode之分治法专题-654. 最大二叉树(Maximum Binary Tree)

    Leetcode之分治法专题-654. 最大二叉树(Maximum Binary Tree) 给定一个不含重复元素的整数数组.一个以此数组构建的最大二叉树定义如下: 二叉树的根是数组中的最大元素. 左 ...

  7. LeetCode - 654. Maximum Binary Tree

    Given an integer array with no duplicates. A maximum tree building on this array is defined as follo ...

  8. [Swift]LeetCode654. 最大二叉树 | Maximum Binary Tree

    Given an integer array with no duplicates. A maximum tree building on this array is defined as follo ...

  9. 654. Maximum Binary Tree 最大节点劈开,然后左边、右边排序

    [抄题]: Given an integer array with no duplicates. A maximum tree building on this array is defined as ...

随机推荐

  1. stl之vector的应用

    这里主要是对vector容器的一些常见应用的总结.至于vector的构造函数及初始化能够參考http://blog.csdn.net/lsh_2013/article/details/21191289 ...

  2. 用 kGDB 调试 Linux 内核

    简介 这个文档记录了用kGDB调试Linux内核的全过程,都是在前人工作基础上的一些总结.以下操作都是基于特定板子来进行,但是大部分都能应用于其他平台. 要使用KGDB来调试内核,首先需要修改conf ...

  3. YTU 2418: C语言习题 矩阵元素变换

    2418: C语言习题 矩阵元素变换 时间限制: 1 Sec  内存限制: 128 MB 提交: 293  解决: 155 题目描述 将一个n×n(2<n<10,n为奇数)的矩阵中最大的元 ...

  4. 欧拉函数与数论的结合UVA11426

    链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&am ...

  5. Html.PartialView(),html.Renderpartial,html.action.html.RenderAction 辅助方法

    Html.Partial(), 返回HTML字符串 .参数为部分视图 html.RenderPartial(),不返回返回HTML字符串 ,直接输出响应流.参数为部分视图 一般用于主视图中已经存在了这 ...

  6. Linux下 本地yum源搭建

    第1章 关于yum源 1.1 什么是yum源 yum(Yellow dog Updater, Modified)是一个在 Fedora 和 RedHat 以及 CentOS 中的 Shell 前端软件 ...

  7. 鸭子类型(Duck Typing)

    鸭子类型(Duck Typing) 动态类型.没有类型检验.不关注类型,关注方法 相当于静态类型语言的多态 这是程序设计中的一种类型推断风格,这种风格适用于动态语言(比如PHP.Python.Ruby ...

  8. Postgresql个人维护库时,出现有用户在连接又找不到这个用户是谁的强制中断连接的方法;

    方法一: 去PostgreSQL目录下/data/pgdata/9.4,找到pg_hba.conf, 修改pg_hba.conf的白名单IP (修改前,最好服务已停止,我是这么操作的) # IPv4 ...

  9. node.js在读取文件时中文乱码问题

    断更很久了........从今天开始会努力的持续更博,积极学习. 言归正传.今天在写node.js的demo时发现一个bug.我在node中读取本地的text文件时,发现英文的内容可以被读取,但是中文 ...

  10. 洛谷P4550 收集邮票(概率期望)

    传送门 神仙题啊……这思路到底是怎么来的…… ps:本题是第$k$次买邮票需要$k$元,而不是买的邮票标号为$k$时花费$k$元 我们设$g[i]$表示现在有$i$张,要买到$n$张的期望张数,设$P ...