1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| /** * Definition for a binary tree node. * type TreeNode struct { * Val int * Left *TreeNode * Right *TreeNode * } */ func reverseOddLevels(root *TreeNode) *TreeNode { var dfs func (node1, node2 *TreeNode, isOdd bool) dfs = func (node1, node2 *TreeNode, isOdd bool) { if node1 == nil || node2 == nil { return } if isOdd { node1.Val, node2.Val = node2.Val, node1.Val } dfs(node1.Left, node2.Right, !isOdd) dfs(node1.Right, node2.Left, !isOdd) } dfs(root.Left, root.Right, true) return root }
|