Leetcode-998 Maximum Binary Tree II(最大二叉树 II)
作者水平有限,所发仅为个人愚见,如有明显谬误,望斧正
此题给出了一棵二叉树,并给出了此二叉树的构建方法,要求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)相关推荐
- LeetCode 654. Maximum Binary Tree最大二叉树 (C++)
题目: Given an integer array with no duplicates. A maximum tree building on this array is defined as f ...
- [Leetcode]@python 107. Binary Tree Level Order Traversal II
题目链接 https://leetcode.com/problems/binary-tree-level-order-traversal-ii/ 题目原文 Given a binary tree, r ...
- LeetCode 107. Binary Tree Level Order Traversal II
LeetCode 107. Binary Tree Level Order Traversal II Solution1:我的答案 比102那道题多了一行代码... /*** Definition f ...
- LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal 由前序和中序遍历建立二叉树 C++...
LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal 由前序和中序遍历建立二叉树 C++ Given preo ...
- LeetCode 110 Balanced Binary Tree 平衡二叉树
LeetCode 110 Balanced Binary Tree Given a binary tree, determine if it is height-balanced. For this ...
- leetcode 971. Flip Binary Tree To Match Preorder Traversal
leetcode 971. Flip Binary Tree To Match Preorder Traversal 题意:给一颗二叉树,再给一个的数组,能否通过交换两个左右两个子节点,使得二叉树的前 ...
- LeetCode 110 Balanced Binary Tree
LeetCode 110 Balanced Binary Tree Problem Description: 判断二叉树是不是平衡二叉树.所谓平衡二叉树,即每个节点的两个子树深度差的绝对值不超过1. ...
- 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 ...
- LeetCode 106. Construct Binary Tree from Inorder and Postorder Traversal
LeetCode 106. Construct Binary Tree from Inorder and Postorder Traversal Solution1:我的答案 仿照105题写的答案 / ...
- LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal
LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal 本博客转载自:http://www.cnblogs.co ...
最新文章
- Java高并发编程:线程池
- 2021 - 9 下旬 数据结构-线性表-循环队列-java实现代码
- 《操作系统》实验报告——进程管理
- 22 | 哈希算法(下):哈希算法在分布式系统中有哪些应用?
- python中 numpy_Python中的Numpy
- servlet提交表单(思考了好多天)
- 正坐标系及矢量知识,点乘与差乘,旋转
- Mybatis3.3.x技术内幕(十一):执行一个Sql命令的完整流程
- Sublime Text3 插件集合
- 计算机网络的通信主体,计算机网络试题及答案
- macOS SwiftUI 进度指示器组件规范之 02 确定进度指标 Determinate Progress Indicators
- 初体验CSDN Chit GPT
- minio实现大文件分片上传+断点续传+预览
- fault tolerance中的错误和故障检测(Error and Fault Detection Mechanisms)
- Watchguard Firebox 配置DKEY动态口令认证
- 约瑟夫环c语言单链表的解题思路,太透彻了:约瑟夫环的三种解法
- Nignx 网关 和 GateWay网关
- C#实现删除自身程序
- AHP层次分析法理解以及项目实战
- cer pem 证书转换_证书cer格式转为pem格式