1. 程式人生 > >LC.226.Invert Binary Tree

LC.226.Invert Binary Tree

problems ack size nbsp space == OS body sta

https://leetcode.com/problems/invert-binary-tree/description/
Invert a binary tree.
4
/ \
2 7
/ \ / \
1 3 6 9
to
4
/ \
7 2
/ \ / \
9 6 3 1
time: o(n) : n nodes
space: o(n): worst case linkedlist and n calling stack


1 public TreeNode invertTree(TreeNode root) {
2         if (root == null
) return null ; 3 TreeNode left = invertTree(root.left) ; 4 TreeNode right = invertTree(root.right) ; 5 root.right = left ; 6 root.left = right ; 7 return root ; 8 }

LC.226.Invert Binary Tree