2023年6月22日发(作者:)
求⼆叉树左右⼦树⾼度差_算法篇:树之树的⾼度算法:这⼀类题⽬很简单,不过却是树的最基本操作之⼀,引申为判断树是不是平衡⼆叉树。⼀般做法是,计算⼆叉树的左右⼦树的⾼度+1,然后取它们的最⼤值或者最⼩值。题⽬1:代码实现:/** * Definition for a binary tree node. * type TreeNode struct { * Val int * Left *TreeNode * Right *TreeNode * } */ // ⼀棵平衡⼆叉树,左右两棵⼦树的⾼度差的执⾏结果:代码实现:/** * Definition for a binary tree node. * type TreeNode struct { * Val int * Left *TreeNode * Right *TreeNode * } */func maxDepth(root *TreeNode) int {执⾏结果:题⽬3:代码实现:/** * Definition for a binary tree node. * type TreeNode struct { * Val int * Left *TreeNode * Right *TreeNode * } */func minDepth(root *TreeNode) int {执⾏结果:题⽬4:代码实现:/** * Definition for a Node. * type Node struct { * Val int * Children []*Node * } */func maxDepth(root *Node) int { if root == nil { return 0 } var hs []int
执⾏结果:
发布者:admin,转转请注明出处:http://www.yc00.com/xiaochengxu/1687381654a5830.html
评论列表(0条)