golang编写二叉树

最近开始找golang 开发工程师职位,针对算法相关二叉树相关常用面试题搞一遍: 

package tree

import (
	"fmt"
)

type BinaryTree struct {
	Value int
	Left  *BinaryTree
	Right *BinaryTree
}

func InitBinaryTree(root *BinaryTree) *BinaryTree {
	l := BinaryTree{}
	r := BinaryTree{}
	root.Left = l.NewBinaryTreeNode(2)
	root.Right = r.NewBinaryTreeNode(3)
	return root
}

func (this *BinaryTree) NewBinaryTreeNode(value int) *BinaryTree {
	this.Value = value
	this.Left = nil
	this.Right = nil
	return this
}

// 计算二叉树节点个数
func GetNodeNum(root *BinaryTree) int {
	if root == nil {
		return 0
	} else {
		return GetNodeNum(root.Left) + GetNodeNum(root.Right) + 1
	}
}

// 计算二叉树的深度
func GetDegree(root *BinaryTree) int {
	if root == nil {
		return 0
	}
	var maxDegree = 0
	if GetDegree(root.Left) > GetDegree(root.Right) {
		maxDegree = GetDegree(root.Left)
	} else {
		maxDegree = GetDegree(root.Right)
	}
	return maxDegree + 1
}

// 前序遍历: 根-> 左子树 -> 右子树
func PreOrder(root *BinaryTree) {
	if root == nil {
		return
	}
	fmt.Printf("%d->", root.Value)
	PreOrder(root.Left)
	PreOrder(root.Right)
}

// 中序: 左子树-> 根 -> 右子树
func InOrder(root *BinaryTree) {
	if root == nil {
		return
	}
	InOrder(root.Left)
	fmt.Printf("%d->", root.Value)
	InOrder(root.Right)
}

// 后序: 左子树-> 右子树 ->  根
func PostOrder(root *BinaryTree) {
	if root == nil {
		return
	}
	PostOrder(root.Left)
	PostOrder(root.Right)
	fmt.Printf("%d->", root.Value)
}

//  求 K 层节点个数
func GetKthNum(root *BinaryTree ,k int ) int {
	if root==nil{
		return 0 
	}
	if k==1{
		return 1 
	}
	return GetKthNum(root.Left,k-1)+GetKthNum(root.Right,k-1)
}

  

猜你喜欢

转载自www.cnblogs.com/shaoyu19900421/p/11628967.html