LeeCode-Invert Binary Tree
Invert a binary tree.
4
/ \
2 7
/ \ / \
1 3 6 9
to
4 / \ 7 2 / \ / \ 9 6 3 1
1 /** 2 * Definition for a binary tree node. 3 * public class TreeNode { 4 * int val; 5 * TreeNode left; 6 * TreeNode right; 7 * TreeNode(int x) { val = x; } 8 * } 9 */ 10 public class Solution { 11 public TreeNode invertTree(TreeNode root) { 12 13 14 if(root!=null) 15 { 16 TreeNode temp = root.left; 17 root.left = root.right; 18 root.right = temp; 19 20 invertTree(root.left); 21 invertTree(root.right); 22 23 return root; 24 } 25 return null; 26 } 27 }
作者:vpoet
出处:http://www.cnblogs.com/vpoet/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
出处:http://www.cnblogs.com/vpoet/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。