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.0145 #2335

Merged
merged 1 commit into from
Feb 11, 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
360 changes: 233 additions & 127 deletions solution/0100-0199/0145.Binary Tree Postorder Traversal/README.md

Large diffs are not rendered by default.

360 changes: 233 additions & 127 deletions solution/0100-0199/0145.Binary Tree Postorder Traversal/README_EN.md

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,15 @@ class Solution {
public:
vector<int> postorderTraversal(TreeNode* root) {
vector<int> ans;
while (root) {
if (!root->right) {
ans.push_back(root->val);
root = root->left;
} else {
TreeNode* next = root->right;
while (next->left && next->left != root) {
next = next->left;
}
if (!next->left) {
ans.push_back(root->val);
next->left = root;
root = root->right;
} else {
next->left = nullptr;
root = root->left;
}
function<void(TreeNode*)> dfs = [&](TreeNode* root) {
if (!root) {
return;
}
}
reverse(ans.begin(), ans.end());
dfs(root->left);
dfs(root->right);
ans.push_back(root->val);
};
dfs(root);
return ans;
}
};
30 changes: 10 additions & 20 deletions solution/0100-0199/0145.Binary Tree Postorder Traversal/Solution.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,16 @@
* Right *TreeNode
* }
*/
func postorderTraversal(root *TreeNode) []int {
var ans []int
for root != nil {
if root.Right == nil {
ans = append([]int{root.Val}, ans...)
root = root.Left
} else {
next := root.Right
for next.Left != nil && next.Left != root {
next = next.Left
}
if next.Left == nil {
ans = append([]int{root.Val}, ans...)
next.Left = root
root = root.Right
} else {
next.Left = nil
root = root.Left
}
func postorderTraversal(root *TreeNode) (ans []int) {
var dfs func(*TreeNode)
dfs = func(root *TreeNode) {
if root == nil {
return
}
dfs(root.Left)
dfs(root.Right)
ans = append(ans, root.Val)
}
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> postorderTraversal(TreeNode root) {
ans = new ArrayList<>();
dfs(root);
return ans;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ def dfs(root):
return
dfs(root.left)
dfs(root.right)
nonlocal ans
ans.append(root.val)

ans = []
Expand Down
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);
Self::dfs(&node.right, res);
res.push(node.val);
Self::dfs(&node.left, ans);
Self::dfs(&node.right, ans);
ans.push(node.val);
}

pub fn postorder_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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,15 @@
*/

function postorderTraversal(root: TreeNode | null): number[] {
if (root == null) return [];
let stack = [];
let ans = [];
let prev = null;
while (root || stack.length) {
while (root) {
stack.push(root);
root = root.left;
const ans: number[] = [];
const dfs = (root: TreeNode | null) => {
if (!root) {
return;
}
root = stack.pop();
if (!root.right || root.right == prev) {
ans.push(root.val);
prev = root;
root = null;
} else {
stack.push(root);
root = root.right;
}
}
dfs(root.left);
dfs(root.right);
ans.push(root.val);
};
dfs(root);
return ans;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* 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> postorderTraversal(TreeNode* root) {
vector<int> ans;
if (!root) {
return ans;
}
stack<TreeNode*> stk;
stk.push(root);
while (stk.size()) {
auto node = stk.top();
stk.pop();
ans.push_back(node->val);
if (node->left) {
stk.push(node->left);
}
if (node->right) {
stk.push(node->right);
}
}
reverse(ans.begin(), ans.end());
return ans;
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func postorderTraversal(root *TreeNode) (ans []int) {
if root == nil {
return
}
stk := []*TreeNode{root}
for len(stk) > 0 {
node := stk[len(stk)-1]
stk = stk[:len(stk)-1]
ans = append(ans, node.Val)
if node.Left != nil {
stk = append(stk, node.Left)
}
if node.Right != nil {
stk = append(stk, node.Right)
}
}
for i, j := 0, len(ans)-1; i < j; i, j = i+1, j-1 {
ans[i], ans[j] = ans[j], ans[i]
}
return
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,17 @@
*/

function postorderTraversal(root: TreeNode | null): number[] {
if (root == null) {
return [];
const ans: number[] = [];
if (!root) {
return ans;
}
const { val, left, right } = root;
return [...postorderTraversal(left), ...postorderTraversal(right), val];
const stk: TreeNode[] = [root];
while (stk.length) {
const { left, right, val } = stk.pop();
ans.push(val);
left && stk.push(left);
right && stk.push(right);
}
ans.reverse();
return ans;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* 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> postorderTraversal(TreeNode* root) {
vector<int> ans;
while (root) {
if (!root->right) {
ans.push_back(root->val);
root = root->left;
} else {
TreeNode* next = root->right;
while (next->left && next->left != root) {
next = next->left;
}
if (next->left != root) {
ans.push_back(root->val);
next->left = root;
root = root->right;
} else {
next->left = nullptr;
root = root->left;
}
}
}
reverse(ans.begin(), ans.end());
return ans;
}
};
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 postorderTraversal(root *TreeNode) (ans []int) {
for root != nil {
if root.Right == nil {
ans = append([]int{root.Val}, ans...)
root = root.Left
} else {
next := root.Right
for next.Left != nil && next.Left != root {
next = next.Left
}
if next.Left == nil {
ans = append([]int{root.Val}, ans...)
next.Left = root
root = root.Right
} else {
next.Left = nil
root = root.Left
}
}
}
return
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,19 @@
*/

function postorderTraversal(root: TreeNode | null): number[] {
const res = [];
while (root != null) {
const ans: number[] = [];
while (root !== null) {
const { val, left, right } = root;
if (right == null) {
res.push(val);
if (right === null) {
ans.push(val);
root = left;
} else {
let next = right;
while (next.left != null && next.left != root) {
while (next.left !== null && next.left !== root) {
next = next.left;
}
if (next.left == null) {
res.push(val);
if (next.left === null) {
ans.push(val);
next.left = root;
root = right;
} else {
Expand All @@ -34,5 +34,5 @@ function postorderTraversal(root: TreeNode | null): number[] {
}
}
}
return res.reverse();
return ans.reverse();
}
Loading