175.Maximum Binary Tree

题目

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].

题目链接

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

思路

递归的思想实现:

  • Step1:找到数组中最大的值并记录其下标为root_index,对应的值生成root对象;
  • Step2:root的左孩子是nums[low,root_index-1];
  • Step3:root的右孩子是nums[index+1,high];
  • 结束递归的条件是 low > high

code

Java


/* 传入的参数low、high分别表示在本次传入的数组nums可用的元素的最小、最大下标值;
*/
public TreeNode constructTree(int[] nums, int low, int high) {
        if (low > high) {//结束递归的条件
            return null;
        }

        int root_index = low;
        int maxvalue = nums[low];
        for (int i = low + 1; i<= high; i++) {
            if (nums[i] > maxvalue) {
                maxvalue = nums[i];
                root_index = i;
            }
        }

        TreeNode root = new TreeNode(nums[root_index]);
        root.left = constructTree(nums, low, root_index-1);
        root.right = constructTree(nums, root_index+1, high);
        return root;

    }

public TreeNode constructMaximumBinaryTree(int[] nums) {
    return constructTree(nums, 0, nums.length-1);

    }

C++

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
private:

     TreeNode* constructTree(vector<int>& nums, int low, int high) {
        if (low > high) {//结束递归的条件
            return NULL;
        }

        int root_index = low;
        int maxvalue = nums[low];
        for (int i = low + 1; i<= high; i++) {
            if (nums[i] > maxvalue) {
                maxvalue = nums[i];
                root_index = i;
            }
        }

        TreeNode* root = new TreeNode(nums[root_index]);
        root->left = constructTree(nums, low, root_index-1);
        root->right = constructTree(nums, root_index+1, high);
        return root;

    }


public:
    TreeNode* constructMaximumBinaryTree(vector<int>& nums) {
        return constructTree(nums, 0, nums.size() - 1);
    }
};

python

def constructTree(self, nums, low, high):
        if low > high :
            return None

        root_index = low
        maxvalue = nums[low]
        for i in range(low,high+1):
            if nums[i] > maxvalue :
                maxvalue = nums[i];
                root_index = i;
        root = TreeNode(nums[root_index])
        root.left = self.constructTree(nums, low, root_index-1);
        root.right = self.constructTree(nums, root_index+1, high);
        return root;      

    def constructMaximumBinaryTree(self, nums):
        """
        :type nums: List[int]
        :rtype: TreeNode
        """
        return self.constructTree(nums, 0, len(nums)-1)
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 、4下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合;、下载 4使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合;、 4下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
As an AI language model, I cannot provide you with the exact solution you need without further details. However, here is a general approach to find the longest distance in a binary tree using Python: 1. Define a class for a binary tree node with attributes for the node value, left child, and right child. 2. Define a function that recursively traverses the binary tree and returns the height of the tree starting from a given node. 3. Define a function that recursively traverses the binary tree and returns the longest distance between two nodes in the tree. 4. In the function defined in step 3, for each node, calculate the longest distance that passes through that node and update the maximum distance seen so far. 5. Call the function defined in step 3 starting from the root node of the binary tree. Here is a sample implementation of the above approach: ``` class Node: def __init__(self, value): self.value = value self.left = None self.right = None def height(root): if root is None: return 0 return 1 + max(height(root.left), height(root.right)) def longest_distance(root): if root is None: return 0 left_height = height(root.left) right_height = height(root.right) left_distance = longest_distance(root.left) right_distance = longest_distance(root.right) return max(left_height + right_height, max(left_distance, right_distance)) root = Node(1) root.left = Node(2) root.right = Node(3) root.left.left = Node(4) root.left.right = Node(5) root.right.left = Node(6) root.right.right = Node(7) print(longest_distance(root)) ``` This code will output the longest distance between any two nodes in the binary tree.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值