LeetCode 226. Invert Binary Tree
原题链接在这里:https://leetcode.com/problems/invert-binary-tree/
题目:
Invert a binary tree.
4 / \ 2 7 / \ / \ 1 3 6 9
to
4 / \ 7 2 / \ / \ 9 6 3 1
题解:
既可以自顶向下,也可以自底向上.
Time Complexity: O(n). Space: O(logn).
AC Java:
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 //Method 1 自顶向下 14 if(root == null){ 15 return root; 16 } 17 TreeNode temp = root.left; 18 root.left = root.right; 19 root.right = temp; 20 invertTree(root.left); 21 invertTree(root.right); 22 return root; 23 */ 24 25 //Method 2 自底向上 26 if(root == null){ 27 return root; 28 } 29 TreeNode tempLeft = root.left; 30 root.left = invertTree(root.right); 31 root.right = invertTree(tempLeft); 32 return root; 33 } 34 }
AC JavaScript:
1 /** 2 * Definition for a binary tree node. 3 * function TreeNode(val) { 4 * this.val = val; 5 * this.left = this.right = null; 6 * } 7 */ 8 /** 9 * @param {TreeNode} root 10 * @return {TreeNode} 11 */ 12 var invertTree = function(root) { 13 if(!root){ 14 return root; 15 } 16 17 var l = invertTree(root.left); 18 var r = invertTree(root.right); 19 root.left = r; 20 root.right = l; 21 return root; 22 };
AC C++:
1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 10 * }; 11 */ 12 class Solution { 13 public: 14 TreeNode* invertTree(TreeNode* root) { 15 if(!root){ 16 return root; 17 } 18 19 invertTree(root->left); 20 invertTree(root->right); 21 std::swap(root->left, root->right); 22 return root; 23 } 24 };
AC Python:
1 # Definition for a binary tree node. 2 # class TreeNode: 3 # def __init__(self, val=0, left=None, right=None): 4 # self.val = val 5 # self.left = left 6 # self.right = right 7 class Solution: 8 def invertTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]: 9 if root: 10 root.left, root.right = self.invertTree(root.right), self.invertTree(root.left) 11 return root
Iteration.
Time Complexity: O(n).
Space: O(logn).
AC C++:
1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 10 * }; 11 */ 12 class Solution { 13 public: 14 TreeNode* invertTree(TreeNode* root) { 15 std::stack<TreeNode*> stk; 16 stk.push(root); 17 18 while(!stk.empty()){ 19 TreeNode* top = stk.top(); 20 stk.pop(); 21 if(top){ 22 stk.push(top->left); 23 stk.push(top->right); 24 std::swap(top->left, top->right); 25 } 26 } 27 28 return root; 29 } 30 };