Invert Binary Tree
Invert a binary tree.
1 1
/ \ / 2 3 => 3 2
/ 4 4
Do it in recursion is acceptable, can you do it without recursion?
/** * Definition of TreeNode: * public class TreeNode { * public int val; * public TreeNode left, right; * public TreeNode(int val) { * this.val = val; * this.left = this.right = null; * } * } */ public class Solution { /** * @param root: a TreeNode, the root of the binary tree * @return: nothing */ public void invertBinaryTree(TreeNode root) { // write your code here if(root==null) return; TreeNode left=root.left; TreeNode right=root.right; root.left=right; root.right=left; if(left!=null) { invertBinaryTree(left); } if(right!=null) { invertBinaryTree(right); } return; } }
Iteration
[lintcode easy]Invert Binary Tree
原文:http://www.cnblogs.com/kittyamin/p/4970600.html