乐趣区

关于算法-数据结构:算法树

type Tree struct {
     data int
     left *Tree
     right *Tree
}
1. 镜像二叉树
func mirrorTree(root *Tree) *Tree {
    if root == nil {return nil}
    root.left, root.right = root.right, root.left
    mirrorTree(root.left)
    mirrorTree(root.right)
}

func mirrorTree(root *Tree) *Tree {
    if root == nil {return nil}
    
    mirrorTree(root.left)
    mirrorTree(root.right)
    root.left, root.right = right, root.left
    return root
}

2. 二叉树最大深度
func maxLength(root *Tree) int {
    if root == nil {return 0}
    reutnr max(maxLenth(root.left,maxLentgh(root.right)), right) +1 
}
3. 合并二叉树
func merge(r1, r2 *Tree) *Tree {
    if r1 == nil {return r2}
    if r2 == nil {return r1}
    r1.val += r2.val
    r1.Left = mergeTrees(r1.Left, r2.Left)
    r1.Right = mergeTrees(r1.Right, r2.Right)
    return r1
}
退出移动版