Skip to content

Commit 409d4c3

Browse files
committed
feat: add python and java solutions to lcci problem: No.17.12
See https://leetcode-cn.com/problems/binode-lcci/
1 parent a54ed54 commit 409d4c3

File tree

6 files changed

+157
-4
lines changed

6 files changed

+157
-4
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@
100100
1. [二叉搜索树的后序遍历序列](/lcof/面试题33.%20二叉搜索树的后序遍历序列/README.md)
101101
1. [二叉树的最近公共祖先](/solution/0200-0299/0235.Lowest%20Common%20Ancestor%20of%20a%20Binary%20Search%20Tree/README.md)
102102
1. [二叉搜索树的最近公共祖先](/solution/0200-0299/0236.Lowest%20Common%20Ancestor%20of%20a%20Binary%20Tree/README.md)
103+
1. [将二叉搜索树转换为单链表](/lcci/17.12.BiNode/README.md)
103104

104105
### 数学
105106

README_EN.md

+1
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ Complete solutions to [LeetCode](https://leetcode-cn.com/problemset/all/), [LCOF
9696
1. [Construct Binary Tree from Inorder and Postorder Traversal](/solution/0100-0199/0106.Construct%20Binary%20Tree%20from%20Inorder%20and%20Postorder%20Traversal/README_EN.md)
9797
1. [Lowest Common Ancestor of a Binary Tree](/solution/0200-0299/0236.Lowest%20Common%20Ancestor%20of%20a%20Binary%20Tree/README_EN.md)
9898
1. [Lowest Common Ancestor of a Binary Search Tree](/solution/0200-0299/0235.Lowest%20Common%20Ancestor%20of%20a%20Binary%20Search%20Tree/README_EN.md)
99+
1. [BiNode](/lcci/17.12.BiNode/README_EN.md)
99100

100101
### Math
101102

lcci/17.12.BiNode/README.md

+53-2
Original file line numberDiff line numberDiff line change
@@ -29,22 +29,73 @@
2929

3030
<!-- 这里可写通用的实现逻辑 -->
3131

32+
递归将左子树、右子树转换为左、右链表 left 和 right。然后将左链表 left 的最后一个结点的 right 指针指向 root,root 的 right 指针指向右链表 right,并将 root 的 left 指针值为空。
33+
3234
<!-- tabs:start -->
3335

3436
### **Python3**
3537

3638
<!-- 这里可写当前语言的特殊实现逻辑 -->
3739

3840
```python
39-
41+
# Definition for a binary tree node.
42+
# class TreeNode:
43+
# def __init__(self, x):
44+
# self.val = x
45+
# self.left = None
46+
# self.right = None
47+
48+
class Solution:
49+
def convertBiNode(self, root: TreeNode) -> TreeNode:
50+
if root is None:
51+
return None
52+
left = self.convertBiNode(root.left)
53+
right = self.convertBiNode(root.right)
54+
if left is None:
55+
root.right = right
56+
return root
57+
res = left
58+
while left and left.right:
59+
left = left.right
60+
left.right = root
61+
root.right = right
62+
root.left = None
63+
return res
4064
```
4165

4266
### **Java**
4367

4468
<!-- 这里可写当前语言的特殊实现逻辑 -->
4569

4670
```java
47-
71+
/**
72+
* Definition for a binary tree node.
73+
* public class TreeNode {
74+
* int val;
75+
* TreeNode left;
76+
* TreeNode right;
77+
* TreeNode(int x) { val = x; }
78+
* }
79+
*/
80+
class Solution {
81+
public TreeNode convertBiNode(TreeNode root) {
82+
if (root == null) return null;
83+
TreeNode left = convertBiNode(root.left);
84+
TreeNode right = convertBiNode(root.right);
85+
if (left == null) {
86+
root.right = right;
87+
return root;
88+
}
89+
TreeNode res = left;
90+
while (left != null && left.right != null) {
91+
left = left.right;
92+
}
93+
left.right = root;
94+
root.right = right;
95+
root.left = null;
96+
return res;
97+
}
98+
}
4899
```
49100

50101
### **...**

lcci/17.12.BiNode/README_EN.md

+51-2
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,62 @@
3535
### **Python3**
3636

3737
```python
38-
38+
# Definition for a binary tree node.
39+
# class TreeNode:
40+
# def __init__(self, x):
41+
# self.val = x
42+
# self.left = None
43+
# self.right = None
44+
45+
class Solution:
46+
def convertBiNode(self, root: TreeNode) -> TreeNode:
47+
if root is None:
48+
return None
49+
left = self.convertBiNode(root.left)
50+
right = self.convertBiNode(root.right)
51+
if left is None:
52+
root.right = right
53+
return root
54+
res = left
55+
while left and left.right:
56+
left = left.right
57+
left.right = root
58+
root.right = right
59+
root.left = None
60+
return res
3961
```
4062

4163
### **Java**
4264

4365
```java
44-
66+
/**
67+
* Definition for a binary tree node.
68+
* public class TreeNode {
69+
* int val;
70+
* TreeNode left;
71+
* TreeNode right;
72+
* TreeNode(int x) { val = x; }
73+
* }
74+
*/
75+
class Solution {
76+
public TreeNode convertBiNode(TreeNode root) {
77+
if (root == null) return null;
78+
TreeNode left = convertBiNode(root.left);
79+
TreeNode right = convertBiNode(root.right);
80+
if (left == null) {
81+
root.right = right;
82+
return root;
83+
}
84+
TreeNode res = left;
85+
while (left != null && left.right != null) {
86+
left = left.right;
87+
}
88+
left.right = root;
89+
root.right = right;
90+
root.left = null;
91+
return res;
92+
}
93+
}
4594
```
4695

4796
### **...**

lcci/17.12.BiNode/Solution.java

+28
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 TreeNode convertBiNode(TreeNode root) {
12+
if (root == null) return null;
13+
TreeNode left = convertBiNode(root.left);
14+
TreeNode right = convertBiNode(root.right);
15+
if (left == null) {
16+
root.right = right;
17+
return root;
18+
}
19+
TreeNode res = left;
20+
while (left != null && left.right != null) {
21+
left = left.right;
22+
}
23+
left.right = root;
24+
root.right = right;
25+
root.left = null;
26+
return res;
27+
}
28+
}

lcci/17.12.BiNode/Solution.py

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode:
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.left = None
6+
# self.right = None
7+
8+
class Solution:
9+
def convertBiNode(self, root: TreeNode) -> TreeNode:
10+
if root is None:
11+
return None
12+
left = self.convertBiNode(root.left)
13+
right = self.convertBiNode(root.right)
14+
if left is None:
15+
root.right = right
16+
return root
17+
res = left
18+
while left and left.right:
19+
left = left.right
20+
left.right = root
21+
root.right = right
22+
root.left = None
23+
return res

0 commit comments

Comments
 (0)