作者水平有限,所发仅为个人愚见,如有明显谬误,望斧正

此题给出了一棵二叉树,并给出了此二叉树的构建方法,要求coder将给出的二叉树还原成一维vector后,加上一个新的val,并再按照题目所给构建方法构建出一棵新的二叉树。则此问题可分解为以下两步:

① 将给出的树还原成一维vector

②将vector加上val后构建新树

其中第二步就是Leetcode-654在解决的事情,给出一个一维vector以后,告诉你构建方法,要求递归构建树。这里就不再多赘述。需要思考的是第一步,如何将给出的树还原成vector。

当给出一个数组要求构建树时,可以很形象的感受到,如果将数组中的相邻元素看作用硬棒连接起来的有质量的小球,而你拎着一个值最大的节点往上提,提到一个很高的高度,其他节点因为重力的缘故,也都向上,而高度也都随之增高但不高于你拎着的值最大的节点。被强调的,值最大的节点,成了根节点,而左边和右边的节点都成了左右子树的节点。所以当用一个扫描仪从左往右从下往上扫描时,最先被扫到的节点必定在原数组的第一个。因此使用中序遍历遍历题目给出的一棵二叉树,就可以得到原数组!这样第一步也就解决了。

 1 class Solution
 2 {
 3     public:
 4         vector<int> nums;
 5         void PreOrder(TreeNode* T)
 6         {
 7             if(T!=NULL)
 8             {
 9                 PreOrder(T->left);
10                 nums.push_back(T->val);
11                 PreOrder(T->right);
12             }
13         }
14         TreeNode* insertIntoMaxTree(TreeNode* root, int val)
15         {
16             PreOrder(root);
17             nums.push_back(val);
18             return constructMaximumBinaryTree(nums);
19         }
20         TreeNode* constructMaximumBinaryTree(vector<int>& nums)
21         {
22             if(nums.size()==0)return NULL;
23             else if(nums.size()==1)
24             {
25                 TreeNode * node=new TreeNode(nums[0]);
26                 return node;
27             }
28             else
29             {
30                 int max=0;
31                 for(int i=0; i<nums.size(); i++)
32                 {
33                     if(nums[i]>nums[max])
34                         max=i;
35                 }
36                 vector<int> left_num,right_num;
37                 for(int i=0; i<max; i++)
38                 {
39                     left_num.push_back(nums[i]);
40                 }
41                 for(int j=max+1; j<nums.size(); j++)
42                 {
43                     right_num.push_back(nums[j]);
44                 }
45                 TreeNode * node=new TreeNode(nums[max]);
46                 node->left=constructMaximumBinaryTree(left_num);
47                 node->right=constructMaximumBinaryTree(right_num);
48                 return node;
49             }
50         }
51 };

Leetcode-998(C++)

执行用时:28ms

转载于:https://www.cnblogs.com/Asurudo/p/10428003.html

Leetcode-998 Maximum Binary Tree II(最大二叉树 II)相关推荐

  1. LeetCode 654. Maximum Binary Tree最大二叉树 (C++)

    题目: Given an integer array with no duplicates. A maximum tree building on this array is defined as f ...

  2. [Leetcode]@python 107. Binary Tree Level Order Traversal II

    题目链接 https://leetcode.com/problems/binary-tree-level-order-traversal-ii/ 题目原文 Given a binary tree, r ...

  3. LeetCode 107. Binary Tree Level Order Traversal II

    LeetCode 107. Binary Tree Level Order Traversal II Solution1:我的答案 比102那道题多了一行代码... /*** Definition f ...

  4. LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal 由前序和中序遍历建立二叉树 C++...

    LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal 由前序和中序遍历建立二叉树 C++ Given preo ...

  5. LeetCode 110 Balanced Binary Tree 平衡二叉树

    LeetCode 110 Balanced Binary Tree Given a binary tree, determine if it is height-balanced. For this ...

  6. leetcode 971. Flip Binary Tree To Match Preorder Traversal

    leetcode 971. Flip Binary Tree To Match Preorder Traversal 题意:给一颗二叉树,再给一个的数组,能否通过交换两个左右两个子节点,使得二叉树的前 ...

  7. LeetCode 110 Balanced Binary Tree

    LeetCode 110 Balanced Binary Tree Problem Description: 判断二叉树是不是平衡二叉树.所谓平衡二叉树,即每个节点的两个子树深度差的绝对值不超过1. ...

  8. LeetCode: 106. Construct Binary Tree from Inorder and Postorder Traversal

    题目 Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume ...

  9. LeetCode 106. Construct Binary Tree from Inorder and Postorder Traversal

    LeetCode 106. Construct Binary Tree from Inorder and Postorder Traversal Solution1:我的答案 仿照105题写的答案 / ...

  10. LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal

    LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal 本博客转载自:http://www.cnblogs.co ...

最新文章

  1. Java高并发编程:线程池
  2. 2021 - 9 下旬 数据结构-线性表-循环队列-java实现代码
  3. 《操作系统》实验报告——进程管理
  4. 22 | 哈希算法(下):哈希算法在分布式系统中有哪些应用?
  5. python中 numpy_Python中的Numpy
  6. servlet提交表单(思考了好多天)
  7. 正坐标系及矢量知识,点乘与差乘,旋转
  8. Mybatis3.3.x技术内幕(十一):执行一个Sql命令的完整流程
  9. Sublime Text3 插件集合
  10. 计算机网络的通信主体,计算机网络试题及答案
  11. macOS SwiftUI 进度指示器组件规范之 02 确定进度指标 Determinate Progress Indicators
  12. 初体验CSDN Chit GPT
  13. minio实现大文件分片上传+断点续传+预览
  14. fault tolerance中的错误和故障检测(Error and Fault Detection Mechanisms)
  15. Watchguard Firebox 配置DKEY动态口令认证
  16. 约瑟夫环c语言单链表的解题思路,太透彻了:约瑟夫环的三种解法
  17. Nignx 网关 和 GateWay网关
  18. C#实现删除自身程序
  19. AHP层次分析法理解以及项目实战
  20. cer pem 证书转换_证书cer格式转为pem格式

热门文章

  1. 配置Git服務器和Windows客戶端
  2. SQLServer之创建辅助XML索引
  3. Java_异常_04_ OutOfMemoryError系列
  4. 性能优化--布局优化技巧
  5. redhat5.8 alt+ctrl+f1 黑屏
  6. 关于linux LVM
  7. SQL Server 行列转换(1)
  8. Coil - Google推荐的协程图片加载库
  9. android -support-v4.jar是什么文件
  10. Android 简单实现订单模块类APP的物流详情页