Created
January 3, 2020 21:26
-
-
Save shixiaoyu/b975c3181f180e6de160d67be3158841 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public TreeNode invertTree(TreeNode root) { | |
return this.helper(root); | |
} | |
// post order | |
private TreeNode helper(TreeNode root) { | |
if (root == null) { | |
return root; | |
} | |
TreeNode l = helper(root.left); | |
TreeNode r = helper(root.right); | |
root.left = r; | |
root.right = l; | |
return root; | |
} | |
// Preorder | |
private TreeNode invertHelper(TreeNode root) { | |
if (root == null) { | |
return null; | |
} | |
TreeNode temp = root.left; | |
root.left = root.right; | |
root.right = temp; | |
this.invertHelper(root.left); | |
this.invertHelper(root.right); | |
return root; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment