-
-
Notifications
You must be signed in to change notification settings - Fork 7.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add solutions to lc problem: No.0545 (#3315)
No.0545.Boundary of Binary Tree
- Loading branch information
Showing
8 changed files
with
943 additions
and
438 deletions.
There are no files selected for viewing
467 changes: 323 additions & 144 deletions
467
solution/0500-0599/0545.Boundary of Binary Tree/README.md
Large diffs are not rendered by default.
Oops, something went wrong.
465 changes: 322 additions & 143 deletions
465
solution/0500-0599/0545.Boundary of Binary Tree/README_EN.md
Large diffs are not rendered by default.
Oops, something went wrong.
59 changes: 59 additions & 0 deletions
59
solution/0500-0599/0545.Boundary of Binary Tree/Solution.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/** | ||
* Definition for a binary tree node. | ||
* struct TreeNode { | ||
* int val; | ||
* TreeNode *left; | ||
* TreeNode *right; | ||
* TreeNode() : val(0), left(nullptr), right(nullptr) {} | ||
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} | ||
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} | ||
* }; | ||
*/ | ||
class Solution { | ||
public: | ||
vector<int> boundaryOfBinaryTree(TreeNode* root) { | ||
auto dfs = [&](auto&& dfs, vector<int>& nums, TreeNode* root, int i) -> void { | ||
if (!root) { | ||
return; | ||
} | ||
if (i == 0) { | ||
if (root->left != root->right) { | ||
nums.push_back(root->val); | ||
if (root->left) { | ||
dfs(dfs, nums, root->left, i); | ||
} else { | ||
dfs(dfs, nums, root->right, i); | ||
} | ||
} | ||
} else if (i == 1) { | ||
if (root->left == root->right) { | ||
nums.push_back(root->val); | ||
} else { | ||
dfs(dfs, nums, root->left, i); | ||
dfs(dfs, nums, root->right, i); | ||
} | ||
} else { | ||
if (root->left != root->right) { | ||
nums.push_back(root->val); | ||
if (root->right) { | ||
dfs(dfs, nums, root->right, i); | ||
} else { | ||
dfs(dfs, nums, root->left, i); | ||
} | ||
} | ||
} | ||
}; | ||
vector<int> ans = {root->val}; | ||
if (root->left == root->right) { | ||
return ans; | ||
} | ||
vector<int> left, right, leaves; | ||
dfs(dfs, left, root->left, 0); | ||
dfs(dfs, leaves, root, 1); | ||
dfs(dfs, right, root->right, 2); | ||
ans.insert(ans.end(), left.begin(), left.end()); | ||
ans.insert(ans.end(), leaves.begin(), leaves.end()); | ||
ans.insert(ans.end(), right.rbegin(), right.rend()); | ||
return ans; | ||
} | ||
}; |
61 changes: 61 additions & 0 deletions
61
solution/0500-0599/0545.Boundary of Binary Tree/Solution.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/** | ||
* Definition for a binary tree node. | ||
* type TreeNode struct { | ||
* Val int | ||
* Left *TreeNode | ||
* Right *TreeNode | ||
* } | ||
*/ | ||
func boundaryOfBinaryTree(root *TreeNode) []int { | ||
ans := []int{root.Val} | ||
if root.Left == root.Right { | ||
return ans | ||
} | ||
|
||
left, leaves, right := []int{}, []int{}, []int{} | ||
|
||
var dfs func(nums *[]int, root *TreeNode, i int) | ||
dfs = func(nums *[]int, root *TreeNode, i int) { | ||
if root == nil { | ||
return | ||
} | ||
if i == 0 { | ||
if root.Left != root.Right { | ||
*nums = append(*nums, root.Val) | ||
if root.Left != nil { | ||
dfs(nums, root.Left, i) | ||
} else { | ||
dfs(nums, root.Right, i) | ||
} | ||
} | ||
} else if i == 1 { | ||
if root.Left == root.Right { | ||
*nums = append(*nums, root.Val) | ||
} else { | ||
dfs(nums, root.Left, i) | ||
dfs(nums, root.Right, i) | ||
} | ||
} else { | ||
if root.Left != root.Right { | ||
*nums = append(*nums, root.Val) | ||
if root.Right != nil { | ||
dfs(nums, root.Right, i) | ||
} else { | ||
dfs(nums, root.Left, i) | ||
} | ||
} | ||
} | ||
} | ||
|
||
dfs(&left, root.Left, 0) | ||
dfs(&leaves, root, 1) | ||
dfs(&right, root.Right, 2) | ||
|
||
ans = append(ans, left...) | ||
ans = append(ans, leaves...) | ||
for i := len(right) - 1; i >= 0; i-- { | ||
ans = append(ans, right[i]) | ||
} | ||
|
||
return ans | ||
} |
110 changes: 44 additions & 66 deletions
110
solution/0500-0599/0545.Boundary of Binary Tree/Solution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,75 +1,53 @@ | ||
/** | ||
* Definition for a binary tree node. | ||
* public class TreeNode { | ||
* int val; | ||
* TreeNode left; | ||
* TreeNode right; | ||
* TreeNode() {} | ||
* TreeNode(int val) { this.val = val; } | ||
* TreeNode(int val, TreeNode left, TreeNode right) { | ||
* this.val = val; | ||
* this.left = left; | ||
* this.right = right; | ||
* } | ||
* } | ||
*/ | ||
class Solution { | ||
private List<Integer> res; | ||
|
||
public List<Integer> boundaryOfBinaryTree(TreeNode root) { | ||
if (root == null) { | ||
return Collections.emptyList(); | ||
} | ||
res = new ArrayList<>(); | ||
|
||
// root | ||
if (!isLeaf(root)) { | ||
res.add(root.val); | ||
} | ||
|
||
// left boundary | ||
TreeNode t = root.left; | ||
while (t != null) { | ||
if (!isLeaf(t)) { | ||
res.add(t.val); | ||
} | ||
t = t.left == null ? t.right : t.left; | ||
} | ||
|
||
// leaves | ||
addLeaves(root); | ||
|
||
// right boundary(reverse order) | ||
Deque<Integer> s = new ArrayDeque<>(); | ||
t = root.right; | ||
while (t != null) { | ||
if (!isLeaf(t)) { | ||
s.offer(t.val); | ||
} | ||
t = t.right == null ? t.left : t.right; | ||
} | ||
while (!s.isEmpty()) { | ||
res.add(s.pollLast()); | ||
} | ||
|
||
// output | ||
return res; | ||
List<Integer> ans = new ArrayList<>(); | ||
ans.add(root.val); | ||
if (root.left == root.right) { | ||
return ans; | ||
} | ||
List<Integer> left = new ArrayList<>(); | ||
List<Integer> leaves = new ArrayList<>(); | ||
List<Integer> right = new ArrayList<>(); | ||
dfs(left, root.left, 0); | ||
dfs(leaves, root, 1); | ||
dfs(right, root.right, 2); | ||
|
||
ans.addAll(left); | ||
ans.addAll(leaves); | ||
Collections.reverse(right); | ||
ans.addAll(right); | ||
return ans; | ||
} | ||
|
||
private void addLeaves(TreeNode root) { | ||
if (isLeaf(root)) { | ||
res.add(root.val); | ||
private void dfs(List<Integer> nums, TreeNode root, int i) { | ||
if (root == null) { | ||
return; | ||
} | ||
if (root.left != null) { | ||
addLeaves(root.left); | ||
} | ||
if (root.right != null) { | ||
addLeaves(root.right); | ||
if (i == 0) { | ||
if (root.left != root.right) { | ||
nums.add(root.val); | ||
if (root.left != null) { | ||
dfs(nums, root.left, i); | ||
} else { | ||
dfs(nums, root.right, i); | ||
} | ||
} | ||
} else if (i == 1) { | ||
if (root.left == root.right) { | ||
nums.add(root.val); | ||
} else { | ||
dfs(nums, root.left, i); | ||
dfs(nums, root.right, i); | ||
} | ||
} else { | ||
if (root.left != root.right) { | ||
nums.add(root.val); | ||
if (root.right != null) { | ||
dfs(nums, root.right, i); | ||
} else { | ||
dfs(nums, root.left, i); | ||
} | ||
} | ||
} | ||
} | ||
|
||
private boolean isLeaf(TreeNode node) { | ||
return node != null && node.left == null && node.right == null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.