Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add solutions to lc problem: No.0103 #2345

Merged
merged 1 commit into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,9 @@ class Solution {
public:
vector<vector<int>> zigzagLevelOrder(TreeNode* root) {
vector<vector<int>> ans;
if (!root) return ans;
if (!root) {
return ans;
}
queue<TreeNode*> q{{root}};
int left = 1;
while (!q.empty()) {
Expand All @@ -152,10 +154,16 @@ public:
auto node = q.front();
q.pop();
t.emplace_back(node->val);
if (node->left) q.push(node->left);
if (node->right) q.push(node->right);
if (node->left) {
q.push(node->left);
}
if (node->right) {
q.push(node->right);
}
}
if (!left) {
reverse(t.begin(), t.end());
}
if (!left) reverse(t.begin(), t.end());
ans.emplace_back(t);
left ^= 1;
}
Expand Down Expand Up @@ -220,23 +228,25 @@ func zigzagLevelOrder(root *TreeNode) (ans [][]int) {
*/

function zigzagLevelOrder(root: TreeNode | null): number[][] {
const res = [];
if (root == null) {
return res;
const ans: number[][] = [];
if (!root) {
return ans;
}
let isDesc = false;
const queue = [root];
while (queue.length !== 0) {
const arr = queue.slice().map(() => {
const { val, left, right } = queue.shift();
left && queue.push(left);
right && queue.push(right);
return val;
});
res.push(isDesc ? arr.reverse() : arr);
isDesc = !isDesc;
const q: TreeNode[] = [root];
let left: number = 1;
while (q.length) {
const t: number[] = [];
const qq: TreeNode[] = [];
for (const { val, left, right } of q) {
t.push(val);
left && qq.push(left);
right && qq.push(right);
}
ans.push(left ? t : t.reverse());
q.splice(0, q.length, ...qq);
left ^= 1;
}
return res;
return ans;
}
```

Expand Down Expand Up @@ -264,34 +274,33 @@ use std::cell::RefCell;
use std::collections::VecDeque;
impl Solution {
pub fn zigzag_level_order(root: Option<Rc<RefCell<TreeNode>>>) -> Vec<Vec<i32>> {
let mut res = vec![];
if root.is_none() {
return res;
}
let mut is_desc = false;
let mut q = VecDeque::new();
q.push_back(root);
while !q.is_empty() {
let mut arr = vec![];
for _ in 0..q.len() {
if let Some(node) = q.pop_front().unwrap() {
let mut node = node.borrow_mut();
arr.push(node.val);
if node.left.is_some() {
q.push_back(node.left.take());
}
if node.right.is_some() {
q.push_back(node.right.take());
let mut ans = Vec::new();
let mut left = true;
if let Some(root_node) = root {
let mut q = VecDeque::new();
q.push_back(root_node);
while !q.is_empty() {
let mut t = Vec::new();
for _ in 0..q.len() {
if let Some(node) = q.pop_front() {
let node_ref = node.borrow();
t.push(node_ref.val);
if let Some(ref left) = node_ref.left {
q.push_back(Rc::clone(left));
}
if let Some(ref right) = node_ref.right {
q.push_back(Rc::clone(right));
}
}
}
if !left {
t.reverse();
}
ans.push(t);
left = !left;
}
if is_desc {
arr.reverse();
}
is_desc = !is_desc;
res.push(arr);
}
res
ans
}
}
```
Expand All @@ -318,20 +327,14 @@ var zigzagLevelOrder = function (root) {
let left = 1;
while (q.length) {
const t = [];
for (let n = q.length; n; --n) {
const node = q.shift();
t.push(node.val);
if (node.left) {
q.push(node.left);
}
if (node.right) {
q.push(node.right);
}
}
if (!left) {
t.reverse();
const qq = [];
for (const { val, left, right } of q) {
t.push(val);
left && qq.push(left);
right && qq.push(right);
}
ans.push(t);
ans.push(left ? t : t.reverse());
q.splice(0, q.length, ...qq);
left ^= 1;
}
return ans;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,9 @@ class Solution {
public:
vector<vector<int>> zigzagLevelOrder(TreeNode* root) {
vector<vector<int>> ans;
if (!root) return ans;
if (!root) {
return ans;
}
queue<TreeNode*> q{{root}};
int left = 1;
while (!q.empty()) {
Expand All @@ -148,10 +150,16 @@ public:
auto node = q.front();
q.pop();
t.emplace_back(node->val);
if (node->left) q.push(node->left);
if (node->right) q.push(node->right);
if (node->left) {
q.push(node->left);
}
if (node->right) {
q.push(node->right);
}
}
if (!left) {
reverse(t.begin(), t.end());
}
if (!left) reverse(t.begin(), t.end());
ans.emplace_back(t);
left ^= 1;
}
Expand Down Expand Up @@ -216,23 +224,25 @@ func zigzagLevelOrder(root *TreeNode) (ans [][]int) {
*/

function zigzagLevelOrder(root: TreeNode | null): number[][] {
const res = [];
if (root == null) {
return res;
const ans: number[][] = [];
if (!root) {
return ans;
}
let isDesc = false;
const queue = [root];
while (queue.length !== 0) {
const arr = queue.slice().map(() => {
const { val, left, right } = queue.shift();
left && queue.push(left);
right && queue.push(right);
return val;
});
res.push(isDesc ? arr.reverse() : arr);
isDesc = !isDesc;
const q: TreeNode[] = [root];
let left: number = 1;
while (q.length) {
const t: number[] = [];
const qq: TreeNode[] = [];
for (const { val, left, right } of q) {
t.push(val);
left && qq.push(left);
right && qq.push(right);
}
ans.push(left ? t : t.reverse());
q.splice(0, q.length, ...qq);
left ^= 1;
}
return res;
return ans;
}
```

Expand Down Expand Up @@ -260,34 +270,33 @@ use std::cell::RefCell;
use std::collections::VecDeque;
impl Solution {
pub fn zigzag_level_order(root: Option<Rc<RefCell<TreeNode>>>) -> Vec<Vec<i32>> {
let mut res = vec![];
if root.is_none() {
return res;
}
let mut is_desc = false;
let mut q = VecDeque::new();
q.push_back(root);
while !q.is_empty() {
let mut arr = vec![];
for _ in 0..q.len() {
if let Some(node) = q.pop_front().unwrap() {
let mut node = node.borrow_mut();
arr.push(node.val);
if node.left.is_some() {
q.push_back(node.left.take());
}
if node.right.is_some() {
q.push_back(node.right.take());
let mut ans = Vec::new();
let mut left = true;
if let Some(root_node) = root {
let mut q = VecDeque::new();
q.push_back(root_node);
while !q.is_empty() {
let mut t = Vec::new();
for _ in 0..q.len() {
if let Some(node) = q.pop_front() {
let node_ref = node.borrow();
t.push(node_ref.val);
if let Some(ref left) = node_ref.left {
q.push_back(Rc::clone(left));
}
if let Some(ref right) = node_ref.right {
q.push_back(Rc::clone(right));
}
}
}
if !left {
t.reverse();
}
ans.push(t);
left = !left;
}
if is_desc {
arr.reverse();
}
is_desc = !is_desc;
res.push(arr);
}
res
ans
}
}
```
Expand All @@ -314,20 +323,14 @@ var zigzagLevelOrder = function (root) {
let left = 1;
while (q.length) {
const t = [];
for (let n = q.length; n; --n) {
const node = q.shift();
t.push(node.val);
if (node.left) {
q.push(node.left);
}
if (node.right) {
q.push(node.right);
}
}
if (!left) {
t.reverse();
const qq = [];
for (const { val, left, right } of q) {
t.push(val);
left && qq.push(left);
right && qq.push(right);
}
ans.push(t);
ans.push(left ? t : t.reverse());
q.splice(0, q.length, ...qq);
left ^= 1;
}
return ans;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ class Solution {
public:
vector<vector<int>> zigzagLevelOrder(TreeNode* root) {
vector<vector<int>> ans;
if (!root) return ans;
if (!root) {
return ans;
}
queue<TreeNode*> q{{root}};
int left = 1;
while (!q.empty()) {
Expand All @@ -22,10 +24,16 @@ class Solution {
auto node = q.front();
q.pop();
t.emplace_back(node->val);
if (node->left) q.push(node->left);
if (node->right) q.push(node->right);
if (node->left) {
q.push(node->left);
}
if (node->right) {
q.push(node->right);
}
}
if (!left) {
reverse(t.begin(), t.end());
}
if (!left) reverse(t.begin(), t.end());
ans.emplace_back(t);
left ^= 1;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,14 @@ var zigzagLevelOrder = function (root) {
let left = 1;
while (q.length) {
const t = [];
for (let n = q.length; n; --n) {
const node = q.shift();
t.push(node.val);
if (node.left) {
q.push(node.left);
}
if (node.right) {
q.push(node.right);
}
const qq = [];
for (const { val, left, right } of q) {
t.push(val);
left && qq.push(left);
right && qq.push(right);
}
if (!left) {
t.reverse();
}
ans.push(t);
ans.push(left ? t : t.reverse());
q.splice(0, q.length, ...qq);
left ^= 1;
}
return ans;
Expand Down
Loading
Loading