Skip to content

Commit

Permalink
feat: add solutions to lc problem: No.0545 (#3315)
Browse files Browse the repository at this point in the history
No.0545.Boundary of Binary Tree
  • Loading branch information
yanglbme authored Jul 24, 2024
1 parent 7840036 commit 9226ec0
Show file tree
Hide file tree
Showing 8 changed files with 943 additions and 438 deletions.
467 changes: 323 additions & 144 deletions solution/0500-0599/0545.Boundary of Binary Tree/README.md

Large diffs are not rendered by default.

465 changes: 322 additions & 143 deletions solution/0500-0599/0545.Boundary of Binary Tree/README_EN.md

Large diffs are not rendered by default.

59 changes: 59 additions & 0 deletions solution/0500-0599/0545.Boundary of Binary Tree/Solution.cpp
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 solution/0500-0599/0545.Boundary of Binary Tree/Solution.go
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 solution/0500-0599/0545.Boundary of Binary Tree/Solution.java
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;
}
}
83 changes: 39 additions & 44 deletions solution/0500-0599/0545.Boundary of Binary Tree/Solution.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,54 +11,49 @@
* @return {number[]}
*/
var boundaryOfBinaryTree = function (root) {
let leftBoundary = function (root, res) {
while (root) {
let curVal = root.val;
if (root.left) {
root = root.left;
} else if (root.right) {
root = root.right;
} else {
break;
}
res.push(curVal);
const ans = [root.val];
if (root.left === root.right) {
return ans;
}

const left = [];
const leaves = [];
const right = [];

const dfs = function (nums, root, i) {
if (!root) {
return;
}
};
let rightBoundary = function (root, res) {
let stk = [];
while (root) {
let curVal = root.val;
if (root.right) {
root = root.right;
} else if (root.left) {
root = root.left;
if (i === 0) {
if (root.left !== root.right) {
nums.push(root.val);
if (root.left) {
dfs(nums, root.left, i);
} else {
dfs(nums, root.right, i);
}
}
} else if (i === 1) {
if (root.left === root.right) {
nums.push(root.val);
} else {
break;
dfs(nums, root.left, i);
dfs(nums, root.right, i);
}
stk.push(curVal);
}
let len = stk.length;
for (let i = 0; i < len; i++) {
res.push(stk.pop());
}
};
let levelBoundary = function (root, res) {
if (root) {
levelBoundary(root.left, res);
if (!root.left && !root.right) {
res.push(root.val);
} else {
if (root.left !== root.right) {
nums.push(root.val);
if (root.right) {
dfs(nums, root.right, i);
} else {
dfs(nums, root.left, i);
}
}
levelBoundary(root.right, res);
}
};
let res = [];
if (root) {
res.push(root.val);
leftBoundary(root.left, res);
if (root.left || root.right) {
levelBoundary(root, res);
}
rightBoundary(root.right, res);
}
return res;

dfs(left, root.left, 0);
dfs(leaves, root, 1);
dfs(right, root.right, 2);
return ans.concat(left).concat(leaves).concat(right.reverse());
};
Loading

0 comments on commit 9226ec0

Please sign in to comment.