首页 > 编程语言 >Problem P01. [算法课分治] 最大二叉树

Problem P01. [算法课分治] 最大二叉树

时间:2022-09-01 23:55:50浏览次数:59  
标签:node rightchild int leftchild P01 二叉树 Problem NULL root

image

需要注意的:

  1. scanf()的返回值是 EOF,输入结束
  2. 通过指针指向左右子树的二叉树构建
#include<iostream>
#include<bits/stdc++.h>
#include<cstdio>

using namespace std;
struct node
{
    int data;
    node *leftchild;
    node *rightchild;

    node(const int& x){
        data = x;
        leftchild=NULL;
        rightchild=NULL;
    }
};

int nums[1005];
node* buildtree(int left, int right){
    if (left>right) return NULL;
    int idmax = left, valmax = nums[idmax];
    for (int i = left+1; i <= right; i++){
        if (valmax<nums[i]){
            valmax = nums[i];
            idmax = i;
        }
    }
    node* root = new node(valmax);
    //cout << root->data << endl;
    root->leftchild = buildtree(left, idmax-1);
    root->rightchild = buildtree(idmax+1, right);
    return root;
}

void qiansearch(node *root)
{
    printf("%d ", (root->data));
    if (root->leftchild==NULL && root->rightchild==NULL){
        return;
    }
    if (root->leftchild==NULL){
        printf("null ");
    }else {
        qiansearch(root->leftchild);
    }
    if (root->rightchild==NULL){
        printf("null ");
    }else {
        qiansearch(root->rightchild);
    }
    return;
}

int main()
{
    int n;
    for(n = 0; 1; n++)
    {
        int ret = scanf("%d", &nums[n]);
        if (ret == EOF)break;
    }
    n--;
    node *root = buildtree(0, n);
    qiansearch(root);
    return 0;
}

标签:node,rightchild,int,leftchild,P01,二叉树,Problem,NULL,root
From: https://www.cnblogs.com/understanding-friends/p/16648260.html

相关文章

  • CF464E The Classic Problem
    传送门思路\(2^{100000}\)?别想了,普通高精度肯定不行但我们发现,求最短路的过程中,其实是用到了比较大小和加法操作细想比较大小的过程,当长度相同的数,我们会先略过前面......
  • MathProblem 76 Two bags and marble problem
    Youchooseoneoftwoidenticallookingbagsatrandom.Onebaghasthreeblackmarblesandonewhitemarble.Theotherhasthreewhitemarblesandoneblackm......
  • MathProblem 71 Nine pearls and a scale problem
    Youhaveninepearls,eightarerealandoneisfake.Alltherealonesweighthesameandthefakeweighslessthantherealones.Usingabalancescaletwice......
  • 力扣 104. 二叉树的最大深度
    104.二叉树的最大深度给定一个二叉树,找出其最大深度。二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。说明: 叶子节点是指没有子节点的节点。示例:给......
  • 力扣 110. 平衡二叉树 [基础+优化]
    110.平衡二叉树给定一个二叉树,判断它是否是高度平衡的二叉树。本题中,一棵高度平衡二叉树定义为:一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过1。......
  • MathProblem 68 Four weights and a scale problem
    Usingabalancescaleandfourweightsyoumustbeabletobalanceanyintegerloadfrom\(1\)to\(40\).Howmuchshouldeachofthefourweightsweigh?Solut......
  • 文献学习-Proofs for Satisfiability Problems
    ProofsforSatisfiabilityProblemsMarijnJ.H.HeuleandArminBiere1TheUniversityofTexasatAustin,UnitedStates2JohannesKeplerUniversity,Linz,Aus......
  • 【数据结构】二叉树-二叉树类别
    满二叉树如果二叉树中除了叶子结点,每个结点的度都为2,则此二叉树称为满二叉树。 完全二叉树1.如果二叉树中除去最后一层节点为满二叉树,且最后一层的结点依次从左到右......
  • leetcode-998. 最大二叉树 II
    998.最大二叉树II图床:blogimg/刷题记录/leetcode/998/刷题代码汇总:https://www.cnblogs.com/geaming/p/16428234.html题目思路看到树就要想到递归。解法/***D......
  • 662. 二叉树最大宽度
    题目描述给你一棵二叉树的根节点root,返回树的最大宽度。树的最大宽度是所有层中最大的宽度。每一层的宽度被定义为该层最左和最右的非空节点(即,两个端点)之间......