LeetCode 二叉树

package main

type TreeNode struct {
	Val int
	Left *TreeNode
	Right *TreeNode
	Next *TreeNode
}
func preorderTraversal1(root *TreeNode) []int {
	res := []int{}
	if root == nil{
		return res
	}
	stack := []*TreeNode{}
	for root != nil || len(stack) != 0{
		if root != nil{
			res = append(res,root.Val)
			stack = append(stack,root)
			root = root.Left
		}else {
			root = stack[len(stack)-1]
			stack = stack[:len(stack)-1]
			root = root.Right
		}
	}
	return res
}

func preorderTraversal2(root *TreeNode) []int {
	res := []int{}
	if root == nil{
		return res
	}
	preOrder(root,&res)
	return res
}
func preOrder(root *TreeNode,res *[]int){
	if root != nil {
		*res = append(*res,root.Val)
		preOrder(root.Left,res)
		//*res = append(*res,root.Val) 中序
		preOrder(root.Right,res)
		//*res = append(*res,root.Val) 后序
	}
}

func infoxorderTraversal(root *TreeNode) []int {
	res := []int{}
	if root == nil{
		return res
	}
	stack := []*TreeNode{}
	for root != nil || len(stack) != 0 {
		if root != nil{
			stack = append(stack,root)
			root = root.Left
		}else {
			root = stack[len(stack)-1]
			stack = stack[:len(stack)-1]
			res = append(res,root.Val)
			root = root.Right
		}
	}
	return res
}

func postorderTraversal(root *TreeNode) []int {

	res := []int{}
	if root == nil {
		return res
	}

	stack := []*TreeNode{}
	var lastTreeNode *TreeNode

	for root != nil || len(stack) != 0 {
		if root != nil{
			stack = append(stack,root)
			root = root.Left
		}
		n := stack[len(stack) - 1]
		if root.Right == nil || root.Right == lastTreeNode {
			res = append(res , n.Val)
			stack = stack [:len(stack) - 1]
			lastTreeNode = n
		}else {
			root = n.Right
		}
	}

	return res
}

func levelOrder(root *TreeNode) []int {
	res := []int{}
	if root == nil{
		return res
	}
	queue := []*TreeNode{}
	queue = append(queue,root)
	for len(queue) > 0{
		length := len(queue)
		for length > 0 {
			length--
			if queue[0].Left != nil{
				queue = append(queue,queue[0].Left)
			}
			if queue[0].Right != nil{
				queue = append(queue,queue[0].Right)
			}
			res = append(res,queue[0].Val)
			queue = queue[1:]
		}
	}
	return res

}

// 中后还原树
func buildTree(inorder []int, postorder []int) *TreeNode {
	idxMap := map[int]int{}
	for i, v := range inorder {
		idxMap[v] = i
	}
	var build func(int, int) *TreeNode
	build = func(inorderLeft, inorderRight int) *TreeNode {
		// 无剩余节点
		if inorderLeft > inorderRight {
			return nil
		}

		// 后序遍历的末尾元素即为当前子树的根节点
		val := postorder[len(postorder)-1]
		postorder = postorder[:len(postorder)-1]
		root := &TreeNode{Val: val}

		// 根据 val 在中序遍历的位置,将中序遍历划分成左右两颗子树
		// 由于我们每次都从后序遍历的末尾取元素,所以要先遍历右子树再遍历左子树
		inorderRootIndex := idxMap[val]
		root.Right = build(inorderRootIndex+1, inorderRight)
		root.Left = build(inorderLeft, inorderRootIndex-1)
		return root
	}
	return build(0, len(inorder)-1)
}

// 前中还原树
func buildTreeBypre(preorder []int, inorder []int) *TreeNode {
	idxMap := map[int]int{}
	for i, v := range inorder {
		idxMap[v] = i
	}
	var build func(int, int) *TreeNode
	build = func(inorderLeft, inorderRight int) *TreeNode {
		// 无剩余节点
		if inorderLeft > inorderRight {
			return nil
		}

		// 前序遍历的末尾元素即为当前子树的根节点
		val := preorder[0]
		preorder = preorder[1:]
		root := &TreeNode{Val: val}

		// 根据 val 在中序遍历的位置,将中序遍历划分成左右两颗子树
		// 由于我们每次都从前序遍历的头部取元素,所以要先遍历左子树再遍历右子树
		inorderRootIndex := idxMap[val]
		root.Left = build(inorderLeft, inorderRootIndex-1)
		root.Right = build(inorderRootIndex+1, inorderRight)
		return root
	}
	return build(0, len(inorder)-1)
}

// 连接树
func levelOrderToConnect(root *TreeNode)(*TreeNode){
	if root == nil{
		return nil
	}

	queue := []*TreeNode{}
	queue = append(queue,root)
	for len(queue) > 0 {
		length := len(queue)
		for length > 0{
			length --
			if queue[0].Left != nil{
				queue = append(queue,queue[0].Left)
			}
			if queue[0].Right != nil{
				queue[0].Left.Next = queue[0].Right
				queue = append(queue,queue[0].Right)
			}

		}
	}
	return root
}

// 230二叉搜索树中第K小的元素
func kthSmallest(root *TreeNode,k int) int {
	if root == nil{
		return 0
	}
	count := 0
	stack := []*TreeNode{}
	for root != nil || len(stack) > 0{
		if root != nil{
			stack  = append(stack,root)
			root = root.Left
		}else {
			root = stack[len(stack)-1]
			count ++
			if count == k{
				return root.Val
			}
			stack = stack[:len(stack)-1]
			root = root.Right
		}
	}
	return 0
}


// 538 把二叉搜索树转换为累加树
func convertBST(root *TreeNode) *TreeNode {
	traverse(root)
	return root
}
var sum = 0
func traverse(root *TreeNode){
	if root != nil {

		traverse(root.Left)
		sum += root.Val;
		root.Val = sum;
		//*res = append(*res,root.Val) 中序
		traverse(root.Right)
		//*res = append(*res,root.Val) 后序
	}
}


// 98 判断是否是BTS
func isValidBST(root *TreeNode) bool {
	return isValidBSTRecursion(root,nil,nil)
}

func isValidBSTRecursion(root ,min ,max *TreeNode)bool{
	if root == nil {
		return true
	}
	if min != nil && root.Val <= min.Val {
		return false
	}
	if max != nil && root.Val >= max.Val {
		return false
	}
	return isValidBSTRecursion(root.Left,min,root) && isValidBSTRecursion(root.Right,root,max)
}

// 700 二叉搜索树中的搜索
func searchBST(root *TreeNode, val int) *TreeNode {
	if root == nil {
		return nil
	}
	if root.Val == val {
		return root
	}
	return inBTS(root.Left,val)
}
func inBTS(root *TreeNode ,val int) *TreeNode {
	if root == nil {
		return nil
	}
	if root.Val == val {
		return root
	} else if root.Val < val {
		return inBTS(root.Right,val)
	} else {
		return inBTS(root.Left,val)
	}
}

// 701. 二叉搜索树中的插入操作
func insertIntoBST(root *TreeNode, val int) *TreeNode {
	if root == nil {
		return &TreeNode{Val: val}
	}
	if root.Val < val {
		root.Right = insertIntoBST(root.Right,val)
	}
	if root.Val > val{
		root.Left = insertIntoBST(root.Left,val)
	}

	return root
}

// 450. 删除二叉搜索树中的节点
func deleteNode(root *TreeNode, key int) *TreeNode {
	if root == nil {
		return nil
	}
	if root.Val == key {
		if root.Left == nil{
			return root.Right
		}
		if root.Right == nil {
			return root.Left
		}
		minNode := getMin(root.Right)
		root.Val = minNode.Val
		root.Right = deleteNode(root.Right,minNode.Val)
	}
	if root.Val > key{
		root.Left = deleteNode(root.Left,key)
	}
	if root.Val < key{
		root.Right = deleteNode(root.Right,key)
	}
	return root
}

func getMin(root *TreeNode) *TreeNode{
	 min := root
	for root != nil{
		min = root
		root = root.Left
	}
	return min
}

// 96.不同的二叉搜索树(Easy)
var Memo = [][]int{}
func numTrees(n int) int {
	Memo = [][]int{}
	for i := 1 ; i <= n+1 ; i ++{
		temp := []int{}
		for j := 1 ; j <= n+1 ; j ++ {
			temp = append(temp,0)
		}
		Memo  = append(Memo,temp)
	}
	return count(1,n)
}
func  count(low int,hight int) int {
	var res int
	if low > hight {
		return 1
	}
	if Memo[low][hight] != 0{
		return Memo[low][hight]
	}

	for i := low ; i <= hight ; i ++{
		left := count(low,i - 1)
		right := count(i + 1,hight)
		res += left * right
	}

	Memo[low][hight] = res

	return res
}


// 654 最大二叉树
func constructMaximumBinaryTree(nums []int) *TreeNode {
	return build(nums)
}
func build(nums []int)  *TreeNode {
	if len(nums) < 1{
		return nil
	}
	max := 0
	maxIndex := 0
	for index ,num := range nums{
		if num > max{
			max = num
			maxIndex = index
		}
	}

	root := &TreeNode{}
	root.Val = max
	root.Left = build(nums[:maxIndex])
	root.Right = build(nums[maxIndex+1:])

	return root

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值