golang满二叉树及遍历

博客主要围绕判断一棵节点层数不超过16层的二叉树是否为满二叉树展开,聚焦于二叉树的判断问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

判断一棵二叉树是不是满二叉树(树的节点层数h不超过16层)

type Node struct {
	val   int
	left  *Node
	right *Node
}
//二叉树深度
func maxDepth(root *Node) int {
	if root == nil {
		return 0
	}
	lhight := maxDepth(root.left)
	rhight := maxDepth(root.right)
	return max(lhight, rhight)
}
func max(a, b int) int {
	if a > b {
		return a + 1
	}
	return b + 1
}
//判断
func isFull(root *Node) bool {
	if root == nil {
		return true
	}
	lheight := maxDepth(root.left)
	rheight := maxDepth(root.right)
	fmt.Println("l:", lheight, "r:", rheight)
	return isFull(root.left) && isFull(root.right) && (lheight == rheight)

	//完全二叉树
	//if math.Abs(float64(Height(root.left)-Height(root.right))) ==1  {
	//	flag = true
	//} else {
	//	flag = false
	//}
	//return flag && isFull(root.left) && isFull(root.right)
}
func main() {
	root := &Node{1, nil, nil}
	n1 := &Node{2, nil, nil}
	n2 := &Node{3, nil, nil}
	n3 := &Node{4, nil, nil}
	n4 := &Node{5, nil, nil}
	//n5 := &Node{6, nil, nil}
	//n6 := &Node{7, nil, nil}

	root.left = n1
	root.right = n2
	n1.left = n3
	n1.right = n4
	//n2.left = n5
	//n2.right = n6
	fmt.Println("前------------")
	PreOrderTraversal(root)
	fmt.Println()
	fmt.Println("中------------")
	MidOrderTraversal(root)
	fmt.Println()
	fmt.Println("后------------")
	PostOrderTraversal(root)
	fmt.Println()
	fmt.Println("层------------")
	LevelOrderTraversal(root)
	fmt.Println("------------")
	fmt.Println(isFull(root))
}
//2.最小深度
//func minDepth(root *Node) int{
//	if root==nil{
//		return 0
//	}
//	lhight := minDepth(root.left)
//	rhight := minDepth(root.right)
//	if root.left==nil{
//		return lhight+1
//	}else if root.right==nil{
//		return rhight+1
//	}else {
//		return min(lhight,rhight)+1
//	}
//}
//func min(a,b int) int {
//	if a<b{
//		return a
//	}
//	return b
//}
// 中序遍历 左 -> 中 -> 右。
func MidOrderTraversal(tree *Node) {
	if tree == nil {
		return
	}

	MidOrderTraversal(tree.left)
	fmt.Printf(" %d -> ", tree.val)
	MidOrderTraversal(tree.right)
}

// 后序遍历   左 -> 右 -> 中。
func PostOrderTraversal(tree *Node) {
	if tree == nil {
		return
	}

	PostOrderTraversal(tree.left)
	PostOrderTraversal(tree.right)
	fmt.Printf(" %d -> ", tree.val)
}

// 前序遍历 遍历顺序
//  中 -> 左 -> 右。
func PreOrderTraversal(tree *Node) {
	if tree == nil {
		return
	}
	fmt.Printf(" %d -> ", tree.val)
	PreOrderTraversal(tree.left)
	PreOrderTraversal(tree.right)
}

// 按层遍历
func LevelOrderTraversal(tree *Node) {
	if tree == nil {
		return
	}

	// 采用队列实现
	queue := make([]*Node, 0)
	queue = append(queue, tree) // queue push
	for len(queue) > 0 {
		tree = queue[0]
		fmt.Printf(" %d -> ", tree.val)

		queue = queue[1:] // queue pop

		if tree.left != nil {
			queue = append(queue, tree.left)
		}
		if tree.right != nil {
			queue = append(queue, tree.right)
		}
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值