LC.226.Invert Binary Tree

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     }

 

posted @ 2018-02-27 23:41  davidnyc  阅读(97)  评论(0)    收藏  举报