Skip to content

Commit 356c7b7

Browse files
committed
sovle problem Flatten Binary Tree To Linked List
1 parent 061e3a0 commit 356c7b7

File tree

5 files changed

+84
-0
lines changed

5 files changed

+84
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@ All solutions will be accepted!
237237
|105|[Construct Binary Tree From Preorder And Inorder Traversal](https://leetcode-cn.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/description/)|[java/py/js](./algorithms/ConstructBinaryTreeFromPreorderAndInorderTraversal)|Medium|
238238
|654|[Maximum Binary Tree](https://leetcode-cn.com/problems/maximum-binary-tree/description/)|[java/py/js](./algorithms/MaximumBinaryTree)|Medium|
239239
|208|[Implement Trie Prefix Tree](https://leetcode-cn.com/problems/implement-trie-prefix-tree/description/)|[java/py/js](./algorithms/ImplementTriePrefixTree)|Medium|
240+
|114|[Flatten Binary Tree To Linked List](https://leetcode-cn.com/problems/flatten-binary-tree-to-linked-list/description/)|[java/py/js](./algorithms/FlattenBinaryTreeToLinkedList)|Medium|
240241

241242
# Database
242243
|#|Title|Solution|Difficulty|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Flatten Binary Tree To Linked List
2+
This problem is easy to solve by DFS
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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 void flatten(TreeNode root) {
12+
LinkedList<TreeNode> rights = new LinkedList<TreeNode>();
13+
14+
while (root != null) {
15+
if (root.left != null && root.right != null) {
16+
rights.addFirst(root.right);
17+
}
18+
19+
if (root.left != null) {
20+
root.right = root.left;
21+
root.left = null;
22+
} else if (root.right == null && rights.size() > 0) {
23+
root.right = rights.removeFirst();
24+
}
25+
root = root.right;
26+
}
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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 {void} Do not return anything, modify root in-place instead.
11+
*/
12+
var flatten = function(root) {
13+
let rights = []
14+
15+
while (root) {
16+
if (root.left && root.right) {
17+
rights.push(root.right)
18+
}
19+
if (root.left) {
20+
root.right = root.left
21+
root.left = null
22+
} else if (!root.right && rights.length > 0) {
23+
root.right = rights.pop()
24+
}
25+
root = root.right
26+
}
27+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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 flatten(self, root):
10+
"""
11+
:type root: TreeNode
12+
:rtype: void Do not return anything, modify root in-place instead.
13+
"""
14+
rights = []
15+
16+
node = root
17+
while node:
18+
if node.left and node.right:
19+
rights.append(node.right)
20+
21+
if node.left:
22+
node.right = node.left
23+
node.left = None
24+
elif len(rights) > 0 and not node.right:
25+
node.right = rights.pop()
26+
node = node.right

0 commit comments

Comments
 (0)