LeetCode 226

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         if(root == null){
13             return root;
14         }else{
15             TreeNode n = root.left;
16             root.left = invertTree(root.right);
17             root.right = invertTree(n);
18             return root;
19         }
20     }
21 }

 

posted @ 2016-04-24 23:12  Juntaran  阅读(143)  评论(0编辑  收藏  举报