Skip to content

Commit 9d09bf8

Browse files
committed
solve problem Invert Binary Tree
1 parent 0c94b0d commit 9d09bf8

File tree

5 files changed

+78
-1
lines changed

5 files changed

+78
-1
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@ All solutions will be accepted!
1515
|292|[Nim Game](https://leetcode-cn.com/problems/nim-game/description/)|[java/py/js](./algorithms/NimGame)|Easy|
1616
|557|[Reverse Words In A String III](https://leetcode-cn.com/problems/reverse-words-in-a-string-iii/description/)|[java/py/js](./algorithms/ReverseWordsInAStringIII)|Easy|
1717
|104|[Maximum Depth Of Binary Tree](https://leetcode-cn.com/problems/maximum-depth-of-binary-tree/description/)|[java/py/js](./algorithms/MaximumDepthOfBinaryTree)|Easy|
18-
|617|[Merge Two Binary Trees](https://leetcode-cn.com/problems/merge-two-binary-trees/description/)|[java/py/js](./algorithms/MergeTwoBinaryTrees)|Easy|
18+
|617|[Merge Two Binary Trees](https://leetcode-cn.com/problems/merge-two-binary-trees/description/)|[java/py/js](./algorithms/MergeTwoBinaryTrees)|Easy|
19+
|226|[Invert Binary Tree](https://leetcode-cn.com/problems/invert-binary-tree/description/)|[java/py/js]|(./algorithms/InvertBinaryTree)|Easy|

algorithms/InvertBinaryTree/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Invert Binary Tree
2+
We can use resursion to solve this, like [this](./solution.py), or use loop to solve this, like [this](./Solution.java)
+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
class Solution {
11+
public TreeNode invertTree(TreeNode root) {
12+
if (root == null) {
13+
return null;
14+
}
15+
List<TreeNode> list = new ArrayList<TreeNode>();
16+
list.add(root);
17+
while (list.size() > 0) {
18+
TreeNode node = list.get(0);
19+
list.remove(node);
20+
TreeNode tempNode = node.left;
21+
node.left = node.right;
22+
node.right = tempNode;
23+
if (node.left != null) {
24+
list.add(node.left);
25+
}
26+
if (node.right != null) {
27+
list.add(node.right);
28+
}
29+
}
30+
return root;
31+
}
32+
}
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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 === null) {
14+
return null
15+
}
16+
let tempNode = root.left
17+
root.left = root.right
18+
root.right = tempNode
19+
20+
root.left = invertTree(root.left)
21+
root.right = invertTree(root.right)
22+
return root
23+
};
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode(object):
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.left = None
6+
# self.right = None
7+
8+
class Solution(object):
9+
def invertTree(self, root):
10+
"""
11+
:type root: TreeNode
12+
:rtype: TreeNode
13+
"""
14+
if root is None:
15+
return None
16+
root.left, root.right = root.right, root.left
17+
root.left = self.invertTree(root.left)
18+
root.right = self.invertTree(root.right)
19+
return root

0 commit comments

Comments
 (0)