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.0094 #2333

Merged
merged 2 commits into from
Feb 10, 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
301 changes: 210 additions & 91 deletions solution/0000-0099/0094.Binary Tree Inorder Traversal/README.md

Large diffs are not rendered by default.

299 changes: 209 additions & 90 deletions solution/0000-0099/0094.Binary Tree Inorder Traversal/README_EN.md

Large diffs are not rendered by default.

26 changes: 8 additions & 18 deletions solution/0000-0099/0094.Binary Tree Inorder Traversal/Solution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,15 @@ class Solution {
public:
vector<int> inorderTraversal(TreeNode* root) {
vector<int> ans;
while (root) {
if (!root->left) {
ans.push_back(root->val);
root = root->right;
} else {
TreeNode* prev = root->left;
while (prev->right && prev->right != root) {
prev = prev->right;
}
if (!prev->right) {
prev->right = root;
root = root->left;
} else {
ans.push_back(root->val);
prev->right = nullptr;
root = root->right;
}
function<void(TreeNode*)> dfs = [&](TreeNode* root) {
if (!root) {
return;
}
}
dfs(root->left);
ans.push_back(root->val);
dfs(root->right);
};
dfs(root);
return ans;
}
};
30 changes: 10 additions & 20 deletions solution/0000-0099/0094.Binary Tree Inorder Traversal/Solution.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,16 @@
* Right *TreeNode
* }
*/
func inorderTraversal(root *TreeNode) []int {
var ans []int
for root != nil {
if root.Left == nil {
ans = append(ans, root.Val)
root = root.Right
} else {
prev := root.Left
for prev.Right != nil && prev.Right != root {
prev = prev.Right
}
if prev.Right == nil {
prev.Right = root
root = root.Left
} else {
ans = append(ans, root.Val)
prev.Right = nil
root = root.Right
}
func inorderTraversal(root *TreeNode) (ans []int) {
var dfs func(*TreeNode)
dfs = func(root *TreeNode) {
if root == nil {
return
}
dfs(root.Left)
ans = append(ans, root.Val)
dfs(root.Right)
}
return ans
dfs(root)
return
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@
* }
*/
class Solution {
private List<Integer> ans;
private List<Integer> ans = new ArrayList<>();

public List<Integer> inorderTraversal(TreeNode root) {
ans = new ArrayList<>();
dfs(root);
return ans;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@
* @return {number[]}
*/
var inorderTraversal = function (root) {
let ans = [];
function dfs(root) {
if (!root) return;
const ans = [];
const dfs = root => {
if (!root) {
return;
}
dfs(root.left);
ans.push(root.val);
dfs(root.right);
}
};
dfs(root);
return ans;
};
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,19 @@
use std::rc::Rc;
use std::cell::RefCell;
impl Solution {
fn dfs(root: &Option<Rc<RefCell<TreeNode>>>, res: &mut Vec<i32>) {
fn dfs(root: &Option<Rc<RefCell<TreeNode>>>, ans: &mut Vec<i32>) {
if root.is_none() {
return;
}
let node = root.as_ref().unwrap().borrow();
Self::dfs(&node.left, res);
res.push(node.val);
Self::dfs(&node.right, res);
Self::dfs(&node.left, ans);
ans.push(node.val);
Self::dfs(&node.right, ans);
}

pub fn inorder_traversal(root: Option<Rc<RefCell<TreeNode>>>) -> Vec<i32> {
let mut res = vec![];
Self::dfs(&root, &mut res);
res
let mut ans = vec![];
Self::dfs(&root, &mut ans);
ans
}
}
15 changes: 11 additions & 4 deletions solution/0000-0099/0094.Binary Tree Inorder Traversal/Solution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,15 @@
*/

function inorderTraversal(root: TreeNode | null): number[] {
if (root == null) {
return [];
}
return [...inorderTraversal(root.left), root.val, ...inorderTraversal(root.right)];
const ans: number[] = [];
const dfs = (root: TreeNode | null) => {
if (!root) {
return;
}
dfs(root.left);
ans.push(root.val);
dfs(root.right);
};
dfs(root);
return ans;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* 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> inorderTraversal(TreeNode* root) {
vector<int> ans;
stack<TreeNode*> stk;
while (root || stk.size()) {
if (root) {
stk.push(root);
root = root->left;
} else {
root = stk.top();
stk.pop();
ans.push_back(root->val);
root = root->right;
}
}
return ans;
}
};
23 changes: 23 additions & 0 deletions solution/0000-0099/0094.Binary Tree Inorder Traversal/Solution2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func inorderTraversal(root *TreeNode) (ans []int) {
stk := []*TreeNode{}
for root != nil || len(stk) > 0 {
if root != nil {
stk = append(stk, root)
root = root.Left
} else {
root = stk[len(stk)-1]
stk = stk[:len(stk)-1]
ans = append(ans, root.Val)
root = root.Right
}
}
return
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
* @return {number[]}
*/
var inorderTraversal = function (root) {
let ans = [],
stk = [];
const stk = [];
const ans = [];
while (root || stk.length > 0) {
if (root) {
stk.push(root);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,20 @@ use std::rc::Rc;
use std::cell::RefCell;
impl Solution {
pub fn inorder_traversal(mut root: Option<Rc<RefCell<TreeNode>>>) -> Vec<i32> {
let mut res = vec![];
let mut stack = vec![];
while root.is_some() || !stack.is_empty() {
let mut ans = vec![];
let mut stk = vec![];
while root.is_some() || !stk.is_empty() {
if root.is_some() {
let next = root.as_mut().unwrap().borrow_mut().left.take();
stack.push(root);
stk.push(root);
root = next;
} else {
let mut node = stack.pop().unwrap();
let mut node = stk.pop().unwrap();
let mut node = node.as_mut().unwrap().borrow_mut();
res.push(node.val);
ans.push(node.val);
root = node.right.take();
}
}
res
ans
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@
*/

function inorderTraversal(root: TreeNode | null): number[] {
const res = [];
const stack = [];
while (root != null || stack.length != 0) {
if (root != null) {
stack.push(root);
const stk: TreeNode[] = [];
const ans: number[] = [];
while (root || stk.length > 0) {
if (root) {
stk.push(root);
root = root.left;
} else {
const { val, right } = stack.pop();
res.push(val);
root = right;
root = stk.pop();
ans.push(root.val);
root = root.right;
}
}
return res;
return ans;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* 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> inorderTraversal(TreeNode* root) {
vector<int> ans;
while (root) {
if (!root->left) {
ans.push_back(root->val);
root = root->right;
} else {
TreeNode* prev = root->left;
while (prev->right && prev->right != root) {
prev = prev->right;
}
if (!prev->right) {
prev->right = root;
root = root->left;
} else {
ans.push_back(root->val);
prev->right = nullptr;
root = root->right;
}
}
}
return ans;
}
};
30 changes: 30 additions & 0 deletions solution/0000-0099/0094.Binary Tree Inorder Traversal/Solution3.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func inorderTraversal(root *TreeNode) (ans []int) {
for root != nil {
if root.Left == nil {
ans = append(ans, root.Val)
root = root.Right
} else {
prev := root.Left
for prev.Right != nil && prev.Right != root {
prev = prev.Right
}
if prev.Right == nil {
prev.Right = root
root = root.Left
} else {
ans = append(ans, root.Val)
prev.Right = nil
root = root.Right
}
}
}
return
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* @return {number[]}
*/
var inorderTraversal = function (root) {
let ans = [];
const ans = [];
while (root) {
if (!root.left) {
ans.push(root.val);
Expand Down
31 changes: 15 additions & 16 deletions solution/0000-0099/0094.Binary Tree Inorder Traversal/Solution3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,25 @@
*/

function inorderTraversal(root: TreeNode | null): number[] {
const res = [];
while (root != null) {
const { val, left, right } = root;
if (left == null) {
res.push(val);
root = right;
const ans: number[] = [];
while (root) {
if (!root.left) {
ans.push(root.val);
root = root.right;
} else {
let mostRight = left;
while (mostRight.right != null && mostRight.right != root) {
mostRight = mostRight.right;
let prev = root.left;
while (prev.right && prev.right != root) {
prev = prev.right;
}
if (mostRight.right == root) {
res.push(val);
mostRight.right = null;
root = right;
if (!prev.right) {
prev.right = root;
root = root.left;
} else {
mostRight.right = root;
root = left;
ans.push(root.val);
prev.right = null;
root = root.right;
}
}
}
return res;
return ans;
}
Loading