Skip to content

Commit 4ded6e0

Browse files
committed
feat: add solutions to lc problem: No.0545. Boundary of Binary Tree
1 parent ac2d21a commit 4ded6e0

File tree

9 files changed

+384
-10
lines changed

9 files changed

+384
-10
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@
125125
- [二叉搜索树的最近公共祖先](/solution/0200-0299/0236.Lowest%20Common%20Ancestor%20of%20a%20Binary%20Tree/README.md)
126126
- [将二叉搜索树转换为单链表](/lcci/17.12.BiNode/README.md)
127127
- [将二叉搜索树转化为排序的双向链表](/solution/0400-0499/0426.Convert%20Binary%20Search%20Tree%20to%20Sorted%20Doubly%20Linked%20List/README.md)
128+
- [二叉树的边界](/solution/0500-0599/0545.Boundary%20of%20Binary%20Tree/README.md)
128129

129130
### 数学
130131

README_EN.md

+1
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ Complete solutions to [LeetCode](https://leetcode-cn.com/problemset/all/), [LCOF
121121
- [Lowest Common Ancestor of a Binary Search Tree](/solution/0200-0299/0235.Lowest%20Common%20Ancestor%20of%20a%20Binary%20Search%20Tree/README_EN.md)
122122
- [BiNode](/lcci/17.12.BiNode/README_EN.md)
123123
- [Convert Binary Search Tree to Sorted Doubly Linked List](/solution/0400-0499/0426.Convert%20Binary%20Search%20Tree%20to%20Sorted%20Doubly%20Linked%20List/README_EN.md)
124+
- [Boundary of Binary Tree](/solution/0500-0599/0545.Boundary%20of%20Binary%20Tree/README_EN.md)
124125

125126
### Math
126127

solution/0100-0199/0199.Binary Tree Right Side View/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,13 @@ class Solution:
4545
def rightSideView(self, root: TreeNode) -> List[int]:
4646
if not root:
4747
return []
48-
q = [root]
48+
q = collections.deque([root])
4949
res = []
5050
while q:
5151
size = len(q)
5252
res.append(q[0].val)
5353
for _ in range(size):
54-
node = q.pop(0)
54+
node = q.popleft()
5555
if node.right:
5656
q.append(node.right)
5757
if node.left:

solution/0100-0199/0199.Binary Tree Right Side View/README_EN.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,13 @@ class Solution:
5454
def rightSideView(self, root: TreeNode) -> List[int]:
5555
if not root:
5656
return []
57-
q = [root]
57+
q = collections.deque([root])
5858
res = []
5959
while q:
6060
size = len(q)
6161
res.append(q[0].val)
6262
for _ in range(size):
63-
node = q.pop(0)
63+
node = q.popleft()
6464
if node.right:
6565
q.append(node.right)
6666
if node.left:

solution/0100-0199/0199.Binary Tree Right Side View/Solution.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ class Solution:
88
def rightSideView(self, root: TreeNode) -> List[int]:
99
if not root:
1010
return []
11-
q = [root]
11+
q = collections.deque([root])
1212
res = []
1313
while q:
1414
size = len(q)
1515
res.append(q[0].val)
1616
for _ in range(size):
17-
node = q.pop(0)
17+
node = q.popleft()
1818
if node.right:
1919
q.append(node.right)
2020
if node.left:

solution/0500-0599/0545.Boundary of Binary Tree/README.md

+128-2
Original file line numberDiff line numberDiff line change
@@ -56,18 +56,70 @@
5656
<li><code>-1000 <= Node.val <= 1000</code></li>
5757
</ul>
5858

59-
6059
## 解法
6160

6261
<!-- 这里可写通用的实现逻辑 -->
6362

63+
分别求根节点、左边界、叶子节点、右边界,依次放入结果数组 res 中。
64+
65+
注意,求右边界的时候,需要逆序结果,这时可以用栈实现。
66+
6467
<!-- tabs:start -->
6568

6669
### **Python3**
6770

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

7073
```python
74+
# Definition for a binary tree node.
75+
# class TreeNode:
76+
# def __init__(self, val=0, left=None, right=None):
77+
# self.val = val
78+
# self.left = left
79+
# self.right = right
80+
class Solution:
81+
def boundaryOfBinaryTree(self, root: TreeNode) -> List[int]:
82+
self.res = []
83+
if not root:
84+
return self.res
85+
# root
86+
if not self.is_leaf(root):
87+
self.res.append(root.val)
88+
89+
# left boundary
90+
t = root.left
91+
while t:
92+
if not self.is_leaf(t):
93+
self.res.append(t.val)
94+
t = t.left if t.left else t.right
95+
96+
# leaves
97+
self.add_leaves(root)
98+
99+
# right boundary(reverse order)
100+
s = []
101+
t = root.right
102+
while t:
103+
if not self.is_leaf(t):
104+
s.append(t.val)
105+
t = t.right if t.right else t.left
106+
while s:
107+
self.res.append(s.pop())
108+
109+
# output
110+
return self.res
111+
112+
def add_leaves(self, root):
113+
if self.is_leaf(root):
114+
self.res.append(root.val)
115+
return
116+
if root.left:
117+
self.add_leaves(root.left)
118+
if root.right:
119+
self.add_leaves(root.right)
120+
121+
def is_leaf(self, node) -> bool:
122+
return node and node.left is None and node.right is None
71123

72124
```
73125

@@ -76,7 +128,81 @@
76128
<!-- 这里可写当前语言的特殊实现逻辑 -->
77129

78130
```java
79-
131+
/**
132+
* Definition for a binary tree node.
133+
* public class TreeNode {
134+
* int val;
135+
* TreeNode left;
136+
* TreeNode right;
137+
* TreeNode() {}
138+
* TreeNode(int val) { this.val = val; }
139+
* TreeNode(int val, TreeNode left, TreeNode right) {
140+
* this.val = val;
141+
* this.left = left;
142+
* this.right = right;
143+
* }
144+
* }
145+
*/
146+
class Solution {
147+
private List<Integer> res;
148+
149+
public List<Integer> boundaryOfBinaryTree(TreeNode root) {
150+
if (root == null) {
151+
return Collections.emptyList();
152+
}
153+
res = new ArrayList<>();
154+
155+
// root
156+
if (!isLeaf(root)) {
157+
res.add(root.val);
158+
}
159+
160+
// left boundary
161+
TreeNode t = root.left;
162+
while (t != null) {
163+
if (!isLeaf(t)) {
164+
res.add(t.val);
165+
}
166+
t = t.left == null ? t.right : t.left;
167+
}
168+
169+
// leaves
170+
addLeaves(root);
171+
172+
// right boundary(reverse order)
173+
Deque<Integer> s = new ArrayDeque<>();
174+
t = root.right;
175+
while (t != null) {
176+
if (!isLeaf(t)) {
177+
s.offer(t.val);
178+
}
179+
t = t.right == null ? t.left : t.right;
180+
}
181+
while (!s.isEmpty()) {
182+
res.add(s.pollLast());
183+
}
184+
185+
// output
186+
return res;
187+
}
188+
189+
private void addLeaves(TreeNode root) {
190+
if (isLeaf(root)) {
191+
res.add(root.val);
192+
return;
193+
}
194+
if (root.left != null) {
195+
addLeaves(root.left);
196+
}
197+
if (root.right != null) {
198+
addLeaves(root.right);
199+
}
200+
}
201+
202+
private boolean isLeaf(TreeNode node) {
203+
return node != null && node.left == null && node.right == null;
204+
}
205+
}
80206
```
81207

82208
### **...**

solution/0500-0599/0545.Boundary of Binary Tree/README_EN.md

+124-2
Original file line numberDiff line numberDiff line change
@@ -57,21 +57,143 @@ Concatenating everything results in [1] + [2] + [4,7,8,9,10] + [6,3] = [1,2,4,7,
5757
<li><code>-1000 &lt;= Node.val &lt;= 1000</code></li>
5858
</ul>
5959

60-
6160
## Solutions
6261

6362
<!-- tabs:start -->
6463

6564
### **Python3**
6665

6766
```python
67+
# Definition for a binary tree node.
68+
# class TreeNode:
69+
# def __init__(self, val=0, left=None, right=None):
70+
# self.val = val
71+
# self.left = left
72+
# self.right = right
73+
class Solution:
74+
def boundaryOfBinaryTree(self, root: TreeNode) -> List[int]:
75+
self.res = []
76+
if not root:
77+
return self.res
78+
# root
79+
if not self.is_leaf(root):
80+
self.res.append(root.val)
81+
82+
# left boundary
83+
t = root.left
84+
while t:
85+
if not self.is_leaf(t):
86+
self.res.append(t.val)
87+
t = t.left if t.left else t.right
88+
89+
# leaves
90+
self.add_leaves(root)
91+
92+
# right boundary(reverse order)
93+
s = []
94+
t = root.right
95+
while t:
96+
if not self.is_leaf(t):
97+
s.append(t.val)
98+
t = t.right if t.right else t.left
99+
while s:
100+
self.res.append(s.pop())
101+
102+
# output
103+
return self.res
104+
105+
def add_leaves(self, root):
106+
if self.is_leaf(root):
107+
self.res.append(root.val)
108+
return
109+
if root.left:
110+
self.add_leaves(root.left)
111+
if root.right:
112+
self.add_leaves(root.right)
113+
114+
def is_leaf(self, node) -> bool:
115+
return node and node.left is None and node.right is None
68116

69117
```
70118

71119
### **Java**
72120

73121
```java
74-
122+
/**
123+
* Definition for a binary tree node.
124+
* public class TreeNode {
125+
* int val;
126+
* TreeNode left;
127+
* TreeNode right;
128+
* TreeNode() {}
129+
* TreeNode(int val) { this.val = val; }
130+
* TreeNode(int val, TreeNode left, TreeNode right) {
131+
* this.val = val;
132+
* this.left = left;
133+
* this.right = right;
134+
* }
135+
* }
136+
*/
137+
class Solution {
138+
private List<Integer> res;
139+
140+
public List<Integer> boundaryOfBinaryTree(TreeNode root) {
141+
if (root == null) {
142+
return Collections.emptyList();
143+
}
144+
res = new ArrayList<>();
145+
146+
// root
147+
if (!isLeaf(root)) {
148+
res.add(root.val);
149+
}
150+
151+
// left boundary
152+
TreeNode t = root.left;
153+
while (t != null) {
154+
if (!isLeaf(t)) {
155+
res.add(t.val);
156+
}
157+
t = t.left == null ? t.right : t.left;
158+
}
159+
160+
// leaves
161+
addLeaves(root);
162+
163+
// right boundary(reverse order)
164+
Deque<Integer> s = new ArrayDeque<>();
165+
t = root.right;
166+
while (t != null) {
167+
if (!isLeaf(t)) {
168+
s.offer(t.val);
169+
}
170+
t = t.right == null ? t.left : t.right;
171+
}
172+
while (!s.isEmpty()) {
173+
res.add(s.pollLast());
174+
}
175+
176+
// output
177+
return res;
178+
}
179+
180+
private void addLeaves(TreeNode root) {
181+
if (isLeaf(root)) {
182+
res.add(root.val);
183+
return;
184+
}
185+
if (root.left != null) {
186+
addLeaves(root.left);
187+
}
188+
if (root.right != null) {
189+
addLeaves(root.right);
190+
}
191+
}
192+
193+
private boolean isLeaf(TreeNode node) {
194+
return node != null && node.left == null && node.right == null;
195+
}
196+
}
75197
```
76198

77199
### **...**

0 commit comments

Comments
 (0)