迭代 递归 反转链表 反转二叉树
https://leetcode-cn.com/problems/reverse-linked-list/
反转链表 迭代 递归
https://leetcode-cn.com/problems/invert-binary-tree/
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func swap(root *TreeNode) {
if root != nil {
t := root.Left
root.Left = root.Right
root.Right = t
}
}
func invertTree(root *TreeNode) *TreeNode {
if root != nil {
swap(root)
invertTree(root.Left)
invertTree(root.Right)
}
return root
}
https://leetcode-cn.com/problems/invert-binary-tree/solution/fan-zhuan-er-cha-shu-by-leetcode-solution/
func invertTree(root *TreeNode) *TreeNode {
if root == nil {
return nil
}
left := invertTree(root.Left)
right := invertTree(root.Right)
root.Left = right
root.Right = left
return root
}
https://leetcode.cn/problems/swap-nodes-in-pairs/solution/liang-liang-jiao-huan-lian-biao-zhong-de-jie-di-91/

浙公网安备 33010602011771号