Skip to content

Commit 6fd076f

Browse files
authored
feat: add solutions to lc problem: No.0145 (doocs#2335)
No.0145.Binary Tree Postorder Traversal
1 parent 8a95663 commit 6fd076f

15 files changed

+653
-376
lines changed

solution/0100-0199/0145.Binary Tree Postorder Traversal/README.md

+233-127
Large diffs are not rendered by default.

solution/0100-0199/0145.Binary Tree Postorder Traversal/README_EN.md

+233-127
Large diffs are not rendered by default.

solution/0100-0199/0145.Binary Tree Postorder Traversal/Solution.cpp

+8-19
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,15 @@ class Solution {
1313
public:
1414
vector<int> postorderTraversal(TreeNode* root) {
1515
vector<int> ans;
16-
while (root) {
17-
if (!root->right) {
18-
ans.push_back(root->val);
19-
root = root->left;
20-
} else {
21-
TreeNode* next = root->right;
22-
while (next->left && next->left != root) {
23-
next = next->left;
24-
}
25-
if (!next->left) {
26-
ans.push_back(root->val);
27-
next->left = root;
28-
root = root->right;
29-
} else {
30-
next->left = nullptr;
31-
root = root->left;
32-
}
16+
function<void(TreeNode*)> dfs = [&](TreeNode* root) {
17+
if (!root) {
18+
return;
3319
}
34-
}
35-
reverse(ans.begin(), ans.end());
20+
dfs(root->left);
21+
dfs(root->right);
22+
ans.push_back(root->val);
23+
};
24+
dfs(root);
3625
return ans;
3726
}
3827
};

solution/0100-0199/0145.Binary Tree Postorder Traversal/Solution.go

+10-20
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,16 @@
66
* Right *TreeNode
77
* }
88
*/
9-
func postorderTraversal(root *TreeNode) []int {
10-
var ans []int
11-
for root != nil {
12-
if root.Right == nil {
13-
ans = append([]int{root.Val}, ans...)
14-
root = root.Left
15-
} else {
16-
next := root.Right
17-
for next.Left != nil && next.Left != root {
18-
next = next.Left
19-
}
20-
if next.Left == nil {
21-
ans = append([]int{root.Val}, ans...)
22-
next.Left = root
23-
root = root.Right
24-
} else {
25-
next.Left = nil
26-
root = root.Left
27-
}
9+
func postorderTraversal(root *TreeNode) (ans []int) {
10+
var dfs func(*TreeNode)
11+
dfs = func(root *TreeNode) {
12+
if root == nil {
13+
return
2814
}
15+
dfs(root.Left)
16+
dfs(root.Right)
17+
ans = append(ans, root.Val)
2918
}
30-
return ans
19+
dfs(root)
20+
return
3121
}

solution/0100-0199/0145.Binary Tree Postorder Traversal/Solution.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,9 @@
1414
* }
1515
*/
1616
class Solution {
17-
private List<Integer> ans;
17+
private List<Integer> ans = new ArrayList<>();
1818

1919
public List<Integer> postorderTraversal(TreeNode root) {
20-
ans = new ArrayList<>();
2120
dfs(root);
2221
return ans;
2322
}

solution/0100-0199/0145.Binary Tree Postorder Traversal/Solution.py

-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ def dfs(root):
1111
return
1212
dfs(root.left)
1313
dfs(root.right)
14-
nonlocal ans
1514
ans.append(root.val)
1615

1716
ans = []

solution/0100-0199/0145.Binary Tree Postorder Traversal/Solution.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,19 @@
1919
use std::rc::Rc;
2020
use std::cell::RefCell;
2121
impl Solution {
22-
fn dfs(root: &Option<Rc<RefCell<TreeNode>>>, res: &mut Vec<i32>) {
22+
fn dfs(root: &Option<Rc<RefCell<TreeNode>>>, ans: &mut Vec<i32>) {
2323
if root.is_none() {
2424
return;
2525
}
2626
let node = root.as_ref().unwrap().borrow();
27-
Self::dfs(&node.left, res);
28-
Self::dfs(&node.right, res);
29-
res.push(node.val);
27+
Self::dfs(&node.left, ans);
28+
Self::dfs(&node.right, ans);
29+
ans.push(node.val);
3030
}
3131

3232
pub fn postorder_traversal(root: Option<Rc<RefCell<TreeNode>>>) -> Vec<i32> {
33-
let mut res = vec![];
34-
Self::dfs(&root, &mut res);
35-
res
33+
let mut ans = vec![];
34+
Self::dfs(&root, &mut ans);
35+
ans
3636
}
3737
}

solution/0100-0199/0145.Binary Tree Postorder Traversal/Solution.ts

+9-18
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,15 @@
1313
*/
1414

1515
function postorderTraversal(root: TreeNode | null): number[] {
16-
if (root == null) return [];
17-
let stack = [];
18-
let ans = [];
19-
let prev = null;
20-
while (root || stack.length) {
21-
while (root) {
22-
stack.push(root);
23-
root = root.left;
16+
const ans: number[] = [];
17+
const dfs = (root: TreeNode | null) => {
18+
if (!root) {
19+
return;
2420
}
25-
root = stack.pop();
26-
if (!root.right || root.right == prev) {
27-
ans.push(root.val);
28-
prev = root;
29-
root = null;
30-
} else {
31-
stack.push(root);
32-
root = root.right;
33-
}
34-
}
21+
dfs(root.left);
22+
dfs(root.right);
23+
ans.push(root.val);
24+
};
25+
dfs(root);
3526
return ans;
3627
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
8+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
9+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
10+
* };
11+
*/
12+
class Solution {
13+
public:
14+
vector<int> postorderTraversal(TreeNode* root) {
15+
vector<int> ans;
16+
if (!root) {
17+
return ans;
18+
}
19+
stack<TreeNode*> stk;
20+
stk.push(root);
21+
while (stk.size()) {
22+
auto node = stk.top();
23+
stk.pop();
24+
ans.push_back(node->val);
25+
if (node->left) {
26+
stk.push(node->left);
27+
}
28+
if (node->right) {
29+
stk.push(node->right);
30+
}
31+
}
32+
reverse(ans.begin(), ans.end());
33+
return ans;
34+
}
35+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* type TreeNode struct {
4+
* Val int
5+
* Left *TreeNode
6+
* Right *TreeNode
7+
* }
8+
*/
9+
func postorderTraversal(root *TreeNode) (ans []int) {
10+
if root == nil {
11+
return
12+
}
13+
stk := []*TreeNode{root}
14+
for len(stk) > 0 {
15+
node := stk[len(stk)-1]
16+
stk = stk[:len(stk)-1]
17+
ans = append(ans, node.Val)
18+
if node.Left != nil {
19+
stk = append(stk, node.Left)
20+
}
21+
if node.Right != nil {
22+
stk = append(stk, node.Right)
23+
}
24+
}
25+
for i, j := 0, len(ans)-1; i < j; i, j = i+1, j-1 {
26+
ans[i], ans[j] = ans[j], ans[i]
27+
}
28+
return
29+
}

solution/0100-0199/0145.Binary Tree Postorder Traversal/Solution2.rs

-43
This file was deleted.

solution/0100-0199/0145.Binary Tree Postorder Traversal/Solution2.ts

+12-4
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,17 @@
1313
*/
1414

1515
function postorderTraversal(root: TreeNode | null): number[] {
16-
if (root == null) {
17-
return [];
16+
const ans: number[] = [];
17+
if (!root) {
18+
return ans;
1819
}
19-
const { val, left, right } = root;
20-
return [...postorderTraversal(left), ...postorderTraversal(right), val];
20+
const stk: TreeNode[] = [root];
21+
while (stk.length) {
22+
const { left, right, val } = stk.pop();
23+
ans.push(val);
24+
left && stk.push(left);
25+
right && stk.push(right);
26+
}
27+
ans.reverse();
28+
return ans;
2129
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
8+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
9+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
10+
* };
11+
*/
12+
class Solution {
13+
public:
14+
vector<int> postorderTraversal(TreeNode* root) {
15+
vector<int> ans;
16+
while (root) {
17+
if (!root->right) {
18+
ans.push_back(root->val);
19+
root = root->left;
20+
} else {
21+
TreeNode* next = root->right;
22+
while (next->left && next->left != root) {
23+
next = next->left;
24+
}
25+
if (next->left != root) {
26+
ans.push_back(root->val);
27+
next->left = root;
28+
root = root->right;
29+
} else {
30+
next->left = nullptr;
31+
root = root->left;
32+
}
33+
}
34+
}
35+
reverse(ans.begin(), ans.end());
36+
return ans;
37+
}
38+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* type TreeNode struct {
4+
* Val int
5+
* Left *TreeNode
6+
* Right *TreeNode
7+
* }
8+
*/
9+
func postorderTraversal(root *TreeNode) (ans []int) {
10+
for root != nil {
11+
if root.Right == nil {
12+
ans = append([]int{root.Val}, ans...)
13+
root = root.Left
14+
} else {
15+
next := root.Right
16+
for next.Left != nil && next.Left != root {
17+
next = next.Left
18+
}
19+
if next.Left == nil {
20+
ans = append([]int{root.Val}, ans...)
21+
next.Left = root
22+
root = root.Right
23+
} else {
24+
next.Left = nil
25+
root = root.Left
26+
}
27+
}
28+
}
29+
return
30+
}

solution/0100-0199/0145.Binary Tree Postorder Traversal/Solution3.ts

+8-8
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,19 @@
1313
*/
1414

1515
function postorderTraversal(root: TreeNode | null): number[] {
16-
const res = [];
17-
while (root != null) {
16+
const ans: number[] = [];
17+
while (root !== null) {
1818
const { val, left, right } = root;
19-
if (right == null) {
20-
res.push(val);
19+
if (right === null) {
20+
ans.push(val);
2121
root = left;
2222
} else {
2323
let next = right;
24-
while (next.left != null && next.left != root) {
24+
while (next.left !== null && next.left !== root) {
2525
next = next.left;
2626
}
27-
if (next.left == null) {
28-
res.push(val);
27+
if (next.left === null) {
28+
ans.push(val);
2929
next.left = root;
3030
root = right;
3131
} else {
@@ -34,5 +34,5 @@ function postorderTraversal(root: TreeNode | null): number[] {
3434
}
3535
}
3636
}
37-
return res.reverse();
37+
return ans.reverse();
3838
}

0 commit comments

Comments
 (0)