代码随想录算法训练营第十五天| 层序遍历、226.翻转二叉树 、 101. 对称二叉树

func levelOrder(root *TreeNode) [][]int {
	arr := [][]int{}
	depth := 0

	var order func(root *TreeNode, depth int)

	order = func(root *TreeNode, depth int) {
		if root == nil {
			return
		}
		if len(arr) == depth {
			arr = append(arr, []int{})
		}
		arr[depth] = append(arr[depth], root.Val)
		order(root.Left, depth+1)
		order(root.Right, depth+1)

	}
	order(root, depth)

	return arr
}

//层序遍历 队列模拟

func levelOrder(root *TreeNode) [][]int {
	res := [][]int{}
	if root == nil {
		return res
	}

	queue := list.New()
	queue.PushBack(root)

	var tmpArr []int
	for queue.Len() > 0 {
		length := queue.Len()

		for i := 0; i < length; i++ {
			node := queue.Remove(queue.Front()).(*TreeNode)
			if node.Left != nil {
				queue.PushBack(node.Left)
			}
			if node.Right != nil {
				queue.PushBack(node.Right)
			}
			tmpArr = append(tmpArr, node.Val)
		}
		res = append(res, tmpArr)
		tmpArr = []int{}
	}
	return res
}

切片模拟

func levelOrder(root *TreeNode) (res [][]int ){

	if root == nil {
		return res
	}


    curLevel:=[]*TreeNode{root}


	for len(curLevel)>0 {
		nextLevel:=[]*TreeNode{}
		vals:=[]int{}

		for _,node := range curLevel{
			vals = append(vals,node.Val)
			if node.Left != nil {
				nextLevel=append(nextLevel,node.Left)
			}
			if node.Right != nil {
				nextLevel=append(nextLevel,node.Right)
			}

		}
		res = append(res, vals)
		curLevel = nextLevel
	}
	return res
}

[226].翻转二叉树

func invertTree(root *TreeNode) *TreeNode {
	if root ==nil{
		return nil
	}
	root.Left,root.Right = root.Right,root.Left
	invertTree(root.Left)
	invertTree(root.Right)

	return root

}	

[111]. 二叉树的最小深度

func minDepth(root *TreeNode) int {
	ans:=0
	if root==nil{
	return 0
	}
	queue:=list.New()
	queue.PushBack(root)
	for queue.Len()>0{
		length:=queue.Len()
		for i := 0; i < length; i++ {
			node:=queue.Remove(queue.Front()).(*TreeNode)
			if node.Left ==nil&&node.Right==nil{
				return ans+1
			}
			if node.Left != nil{
				queue.PushBack(node.Left)
			}
			if node.Right !=nil{
				queue.PushBack(node.Right)
			}
			
		}
		ans++
	}
	return ans+1
	

}

[104].二叉树的最大深度

func maxDepth(root *TreeNode) int {
	ans:=0
	if root==nil{
	return 0
	}
	queue:=list.New()
	queue.PushBack(root)
	for queue.Len()>0{
		length:=queue.Len()
		for i := 0; i < length; i++ {
			node:=queue.Remove(queue.Front()).(*TreeNode)
			// if node.Left ==nil&&node.Right==nil{
			// 	return ans+1
			// }
			if node.Left != nil{
				queue.PushBack(node.Left)
			}
			if node.Right !=nil{
				queue.PushBack(node.Right)
			}
			
		}
		ans++
	}
	return ans
	

}

[116].填充每个节点的下一个右侧节点指针

func connect(root *Node) *Node {

	if root==nil{
	return root
	}
	queue:=list.New()
	queue.PushBack(root)
	temArr:=make([]*Node, 0)
	for queue.Len()>0{
		length:=queue.Len()
		for i := 0; i < length; i++ {
			node:=queue.Remove(queue.Front()).(*Node)
		
			if node.Left != nil{
				queue.PushBack(node.Left)
			}
			if node.Right !=nil{
				queue.PushBack(node.Right)
			}

			temArr = append(temArr,node)
			
		}
		if len(temArr)>1{
			for i := 0; i < len(temArr)-1; i++ {
				temArr[i].Next= temArr[i+1]
				
			}
		}
		temArr = []*Node{}

	}
	return root

	
}

[117] 填充每个节点的下一个右侧节点指针 II


func connect(root *Node) *Node {
	if root==nil{
		return root
	}
	queue := list.New()
	queue.PushBack(root)
	temArr := make([]*Node,0)
	for queue.Len()>0 {
		length:=queue.Len()
		for i := 0; i < length; i++ {
			
		node:=queue.Remove(queue.Front()).(*Node)
		
		if node.Left!=nil{
			queue.PushBack(node.Left)
		}
		if node.Right!=nil{
			queue.PushBack(node.Right)
		}

		temArr =append(temArr,node)
		
	}
	if len(temArr)>1{
	for i := 0; i < len(temArr)-1; i++ {
		temArr[i].Next = temArr[i+1]
		
	}
}
	temArr=[]*Node{}
}
return root

	
}

[515].在每个树行中找最大值

func largestValues(root *TreeNode) []int {
	if root==nil{
		return []int{}
	}
	queue := list.New()
	queue.PushBack(root)
	temArr := make([]*TreeNode,0)
	ans:=make([]int, 0)
	temp:=math.MinInt64
	for queue.Len()>0 {
		length:=queue.Len()
		for i := 0; i < length; i++ {
			
		node:=queue.Remove(queue.Front()).(*TreeNode)
		temp = max(temp,node.Val)

		if node.Left!=nil{
			queue.PushBack(node.Left)
		}
		if node.Right!=nil{
			queue.PushBack(node.Right)
		}

		temArr =append(temArr,node)
		
	}
	ans:=append(ans,temp)
	temp =math.MinInt64
}


return ans
}
func  max(i,j int) int {
	if i>j{
		return i
	}
	return j
	
}

[429].N 叉树的层序遍历

func levelOrder(root *Node) [][]int {
	queue := list.New()
	res := [][]int{}
	if root == nil {
		return res
	}

	queue.PushBack(root)

	for queue.Len() > 0 {
		length := queue.Len()
		var temp []int

		for i := 0; i < length; i++ {
			myNode := queue.Remove(queue.Front()).(*Node)
			temp = append(temp, myNode.Val)
			for j := 0; j < len(myNode.Children); j++ {
				queue.PushBack(myNode.Children[j])

			}

		}
		res = append(res, temp)

	}
	return res

}

[101].对称二叉树

func isSymmetric(root *TreeNode) bool {
	return dfs(root.Left,root.Right)

}

func dfs(left *TreeNode,right *TreeNode)bool{
	if left ==nil&&right==nil{
		return true
	}
	if left ==nil||right==nil{
		return false
	}
	if left.Val !=right.Val{
		return false
	}
	return dfs(left.Left,right.Right)&&dfs(left.Right,right.Left)

}

//迭代法

func isSymmetric(root *TreeNode) bool {
	var queue []*TreeNode

	if root != nil {
		queue = append(queue, root.Left, root.Right)
	}
	for len(queue) > 0 {
		left := queue[0]
		right := queue[1]
		queue = queue[2:]

		if left == nil && right == nil {
			continue
		}
		if left == nil || right == nil || left.Val != right.Val {
			return false
		}
		queue = append(queue, left.Left, right.Right, left.Right,right.Left)
	}
	return true

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值