首页 > 其他 > 详细

LC.226.Invert Binary Tree

时间:2018-02-28 00:07:18      阅读:220      评论:0      收藏:0      [点我收藏+]
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

原文:https://www.cnblogs.com/davidnyc/p/8481581.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!