Code Monkey home page Code Monkey logo

Comments (3)

moutainMan avatar moutainMan commented on June 14, 2024

代码如下:

package main

import "fmt"

// 定义二叉树结构体
type treeNode struct {
    val   byte      // 节点的值
    left  *treeNode // 左子树
    right *treeNode // 右子树
}

// 先序遍历和中序遍历构建二叉树
func buildTree(preorder, inorder []byte) *treeNode {
    if len(preorder) == 0 {
        return nil
    }

    // 先序遍历的第一个节点是根节点
    root := &treeNode{
        val: preorder[0],
    }

    // 找到根节点在中序遍历序列中的位置
    i := 0
    for ; i < len(inorder); i++ {
        if inorder[i] == root.val {
            break
        }
    }

    // 递归构建左右子树
    root.left = buildTree(preorder[1:1+i], inorder[:i])
    root.right = buildTree(preorder[1+i:], inorder[i+1:])

    return root
}

// 计算二叉树的高度(深度)
func height(root *treeNode) int {
    if root == nil {
        return 0
    }

    leftHeight := height(root.left)
    rightHeight := height(root.right)

    if leftHeight > rightHeight {
        return leftHeight + 1
    } else {
        return rightHeight + 1
    }
}

func main() {
    var k int
    for {
        _, err := fmt.Scan(&k)
        if err != nil {
            break
        }

        preorder := make([]byte, k)
        inorder := make([]byte, k)

        fmt.Scan(&preorder, &inorder)

        // 构建二叉树
        root := buildTree(preorder, inorder)

        // 计算二叉树高度
        fmt.Println(height(root))
    }
}

from kamacoder-solutions.

youngyangyang04 avatar youngyangyang04 commented on June 14, 2024

重复代码不要重复提交,系统判断是重复代码后,会直接随机返回结果,不会判题了

from kamacoder-solutions.

moutainMan avatar moutainMan commented on June 14, 2024

OK

from kamacoder-solutions.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.