【LeetCode】654. Maximum Binary Tree
来源:互联网 发布:彩票数据分析预测软件 编辑:程序博客网 时间:2024/06/06 14:22
Given an integer array with no duplicates. A maximum tree building on this array is defined as follow:
- The root is the maximum number in the array.
- The left subtree is the maximum tree constructed from left part subarray divided by the maximum number.
- The right subtree is the maximum tree constructed from right part subarray divided by the maximum number.
Construct the maximum tree by the given array and output the root node of this tree.
Example 1:
Input: [3,2,1,6,0,5]Output: return the tree root node representing the following tree: 6 / \ 3 5 \ / 2 0 \ 1
Note:
- The size of the given array will be in the range [1,1000].
递归题目
/** * Definition for a binary tree node. * function TreeNode(val) { * this.val = val; * this.left = this.right = null; * } *//** * @param {number[]} nums * @return {TreeNode} */function findMaxIdx(nums, begin, end){ let ret = begin; for(let i=begin+1; i<=end; i++){ if(nums[ret] < nums[i]){ ret = i; } } return ret;}function createMaxBinaryTree(nums, begin, end){ if(begin > end){ return null; } let maxIdx = findMaxIdx(nums, begin, end); let root = new TreeNode(nums[maxIdx]); root.left = createMaxBinaryTree(nums, begin, maxIdx-1); root.right = createMaxBinaryTree(nums, maxIdx+1, end); return root;}var constructMaximumBinaryTree = function(nums) { //special case if(!nums || nums.length === 0){ return null; } return createMaxBinaryTree(nums, 0, nums.length-1);};
阅读全文
0 0
- leetcode 654. Maximum Binary Tree
- 【LeetCode】654. Maximum Binary Tree
- [LeetCode]654. Maximum Binary Tree
- leetcode 654. Maximum Binary Tree
- leetcode 654. Maximum Binary Tree
- Leetcode: 654. Maximum Binary Tree
- LeetCode 654. Maximum Binary Tree
- [LeetCode] 654. Maximum Binary Tree
- [leetcode] 654. Maximum Binary Tree
- [LeetCode] 654. Maximum Binary Tree
- LeetCode Maximum Binary Tree
- 654. Maximum Binary Tree
- 654. Maximum Binary Tree
- 654. Maximum Binary Tree
- 654. Maximum Binary Tree
- 654. Maximum Binary Tree
- 654. Maximum Binary Tree
- 654. Maximum Binary Tree
- 吴恩达机器学习课程笔记——第二周
- 利用GitHub搭建个人网站
- InputMethodService详解
- Java 字节流InputStream、OutputStream、FileInputStream、FileOutputStream
- AS 代码混淆+res混淆
- 【LeetCode】654. Maximum Binary Tree
- Matlab函数bwmorph简介 图像处理
- HDU—3791 二叉搜索树 (可以用数组,,,,建树)
- SpringMVC
- 基于STM32+机智云物联网平台的家电状态显示及控制系统
- (14)问卷调查:两种方式className、div样式属性值改变、单选按钮性格测试
- git clone 遇到"unable to access '……':error setting certificate verify locations"问题
- Android开发--与后台通信(一)--API数据获取
- Js 执行上下文