type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}
- 前序遍历(中序遍历、后续遍历只需要调整append的顺序即可)
func preOrder(root *TreeNode) []int {
var result []int
if root != nil {
result = append(result, root.Val)
result = append(result, preOrder(root.Left)...)
result = append(result, preOrder(root.Right)...)
}
return result
}
//dfs解法
func levelOrder(root *TreeNode) [][]int {
var res [][]int
var dfs func(root *TreeNode, level int)
dfs = func(root *TreeNode, level int) {
if root != nil {
if len(res) == level {
res = append(res, []int{})
}
res[level] = append(res[level], root.Val)
dfs(root.Left, level+1)
dfs(root.Right, level+1)
}
}
dfs(root,0)
return res
}
// var test [][]int
// fmt.Println(test)
// test = append(test, []int{})
// fmt.Println(test)
// 结果不一样
//bfs解法
func levelOrder(root *TreeNode) [][]int {
var res [][]int
if root == nil{
return res
}
var queue = []*TreeNode{root}
var level int
for len(queue) > 0 {
counter := len(queue)
res = append(res, []int{})
for counter > 0 {
counter--
if queue[0].Left != nil {
queue = append(queue, queue[0].Left)
}
if queue[0].Right != nil {
queue = append(queue, queue[0].Right)
}
res[level] = append(res[level], queue[0].Val)
queue = queue[1:]
}
level++
}
return res
}
//递归解法
func maxDepth(root *TreeNode) int {
if root != nil {
return int(math.Max(float64(maxDepth(root.Left)+1),float64(maxDepth(root.Right)+1)))
}
return 0
}
//bfs解法
func maxDepth(root *TreeNode) int {
if root == nil {
return 0
}
var queue []*TreeNode
queue = append(queue, root)
var level int
for len(queue) > 0 {
level++
counter := len(queue)
for counter > 0 {
counter--
if queue[0].Left != nil {
queue = append(queue, queue[0].Left)
}
if queue[0].Right != nil {
queue = append(queue, queue[0].Right)
}
queue = queue[1:]
}
}
return level
}
//dfs解法
func maxDepth(root *TreeNode) int {
if root == nil {
return 0
}
maxLevel := 1
var dfs func(root *TreeNode, level int)
dfs = func(root *TreeNode, level int) {
if root != nil {
if level > maxLevel {
maxLevel = level
}
dfs(root.Left, level+1)
dfs(root.Right, level+1)
}
}
dfs(root,1)
return maxLevel
}
func isSymmetric(root *TreeNode) bool {
if root == nil {
return true
} else {
return same(root.Left,root.Right)
}
}
func same(left *TreeNode,right *TreeNode) bool {
if left == nil && right ==nil {
return true
} else if left ==nil || right == nil {
return false
} else if left.Val == right.Val {
return (same(left.Left,right.Right) && same(right.Left,left.Right))
} else {
return false
}
}
//给定一个二叉树和一个目标和,判断该树中是否存在根节点到叶子节点的路径,这条路径上所有节点值相加等于目标和。
func hasPathSum(root *TreeNode, sum int) bool {
if root == nil {
return false
}
if root.Left == nil && root.Right == nil {
return (sum - root.Val) == 0
}
return hasPathSum(root.Left, sum-root.Val) || hasPathSum(root.Right, sum-root.Val)
}
func lowestCommonAncestor(root, p, q *TreeNode) *TreeNode {
if root == nil || root == p || root == q {
return root
}
left := lowestCommonAncestor(root.Left, p, q)
right := lowestCommonAncestor(root.Right, p, q)
if left == nil {
return right
} else if right == nil {
return left
} else {
return root
}
}
网友评论