Update preorder_traversal_iii.

This commit is contained in:
krahets
2023-07-25 16:39:38 +08:00
parent 90af225dae
commit b067016bfa
20 changed files with 48 additions and 56 deletions

View File

@@ -37,7 +37,6 @@ void backtrack(vector<TreeNode *> &state, vector<TreeNode *> &choices, vector<ve
if (isSolution(state)) {
// 记录解
recordSolution(state, res);
return;
}
// 遍历所有选择
for (TreeNode *choice : choices) {

View File

@@ -41,7 +41,7 @@ public class preorder_traversal_iii_compact {
res = new List<List<TreeNode>>();
preOrder(root);
Console.WriteLine("\n输出所有根节点到节点 7 的路径,路径中不包含值为 3 的节点,仅包含一个值为 7 的节点");
Console.WriteLine("\n输出所有根节点到节点 7 的路径,路径中不包含值为 3 的节点");
foreach (List<TreeNode> path in res) {
PrintUtil.PrintList(path.Select(p => p.val).ToList());
}

View File

@@ -38,7 +38,6 @@ public class preorder_traversal_iii_template {
if (isSolution(state)) {
// 记录解
recordSolution(state, res);
return;
}
// 遍历所有选择
foreach (TreeNode choice in choices) {

View File

@@ -59,7 +59,7 @@ func TestPreorderTraversalIIICompact(t *testing.T) {
res := make([][]*TreeNode, 0)
preOrderIII(root, &res, &path)
fmt.Println("\n输出所有根节点到节点 7 的路径,路径中不包含值为 3 的节点,仅包含一个值为 7 的节点")
fmt.Println("\n输出所有根节点到节点 7 的路径,路径中不包含值为 3 的节点")
for _, path := range res {
for _, node := range path {
fmt.Printf("%v ", node.Val)
@@ -81,7 +81,7 @@ func TestPreorderTraversalIIITemplate(t *testing.T) {
choices = append(choices, root)
backtrackIII(&state, &choices, &res)
fmt.Println("\n输出所有根节点到节点 7 的路径,路径中不包含值为 3 的节点,仅包含一个值为 7 的节点")
fmt.Println("\n输出所有根节点到节点 7 的路径,路径中不包含值为 3 的节点")
for _, path := range res {
for _, node := range path {
fmt.Printf("%v ", node.Val)

View File

@@ -43,7 +43,7 @@ public class preorder_traversal_iii_compact {
res = new ArrayList<>();
preOrder(root);
System.out.println("\n输出所有根节点到节点 7 的路径,路径中不包含值为 3 的节点,仅包含一个值为 7 的节点");
System.out.println("\n输出所有根节点到节点 7 的路径,路径中不包含值为 3 的节点");
for (List<TreeNode> path : res) {
List<Integer> vals = new ArrayList<>();
for (TreeNode node : path) {

View File

@@ -41,7 +41,6 @@ public class preorder_traversal_iii_template {
if (isSolution(state)) {
// 记录解
recordSolution(state, res);
return;
}
// 遍历所有选择
for (TreeNode choice : choices) {

View File

@@ -37,7 +37,7 @@ const path = [];
const res = [];
preOrder(root, path, res);
console.log('\n输出所有根节点到节点 7 的路径,路径中不包含值为 3 的节点,仅包含一个值为 7 的节点');
console.log('\n输出所有根节点到节点 7 的路径,路径中不包含值为 3 的节点');
res.forEach((path) => {
console.log(path.map((node) => node.val));
});

View File

@@ -38,7 +38,6 @@ function backtrack(state, choices, res) {
if (isSolution(state)) {
// 记录解
recordSolution(state, res);
return;
}
// 遍历所有选择
for (const choice of choices) {

View File

@@ -39,6 +39,6 @@ if __name__ == "__main__":
res = list[list[TreeNode]]()
pre_order(root)
print("\n输出所有根节点到节点 7 的路径,路径中不包含值为 3 的节点,仅包含一个值为 7 的节点")
print("\n输出所有根节点到节点 7 的路径,路径中不包含值为 3 的节点")
for path in res:
print([node.val for node in path])

View File

@@ -43,7 +43,6 @@ def backtrack(
if is_solution(state):
# 记录解
record_solution(state, res)
return
# 遍历所有选择
for choice in choices:
# 剪枝:检查选择是否合法

View File

@@ -9,7 +9,7 @@ from .list_node import (
linked_list_to_list,
get_list_node,
)
from .tree_node import TreeNode, list_to_tree, tree_to_list, get_tree_node
from .tree_node import TreeNode, list_to_tree, tree_to_list
from .vertex import Vertex, vals_to_vets, vets_to_vals
from .print_util import (
print_matrix,

View File

@@ -67,14 +67,3 @@ def tree_to_list(root: TreeNode | None) -> list[int]:
res = []
tree_to_list_dfs(root, 0, res)
return res
def get_tree_node(root: TreeNode | None, val: int) -> TreeNode | None:
"""Get a tree node with specific value in a binary tree"""
if not root:
return
if root.val == val:
return root
left: TreeNode | None = get_tree_node(root.left, val)
right: TreeNode | None = get_tree_node(root.right, val)
return left if left else right

View File

@@ -42,7 +42,7 @@ pub fn main() {
let mut res = Vec::new();
pre_order(&mut res, &mut path, root);
println!("\n输出所有根节点到节点 7 的路径,路径中不包含值为 3 的节点,仅包含一个值为 7 的节点");
println!("\n输出所有根节点到节点 7 的路径,路径中不包含值为 3 的节点");
for path in res {
let mut vals = Vec::new();
for node in path {

View File

@@ -40,7 +40,6 @@ fn backtrack(state: &mut Vec<Rc<RefCell<TreeNode>>>, choices: &mut Vec<Rc<RefCel
if is_solution(state) {
// 记录解
record_solution(state, res);
return;
}
// 遍历所有选择
for choice in choices {

View File

@@ -42,7 +42,7 @@ enum PreorderTraversalIIICompact {
res = []
preOrder(root: root)
print("\n输出所有根节点到节点 7 的路径,路径中不包含值为 3 的节点,仅包含一个值为 7 的节点")
print("\n输出所有根节点到节点 7 的路径,路径中不包含值为 3 的节点")
for path in res {
var vals: [Int] = []
for node in path {

View File

@@ -36,7 +36,6 @@ func backtrack(state: inout [TreeNode], choices: [TreeNode], res: inout [[TreeNo
//
if isSolution(state: state) {
recordSolution(state: state, res: &res)
return
}
//
for choice in choices {
@@ -65,7 +64,7 @@ enum PreorderTraversalIIITemplate {
var res: [[TreeNode]] = []
backtrack(state: &state, choices: [root].compactMap { $0 }, res: &res)
print("\n输出所有根节点到节点 7 的路径,路径中不包含值为 3 的节点,仅包含一个值为 7 的节点")
print("\n输出所有根节点到节点 7 的路径,路径中不包含值为 3 的节点")
for path in res {
var vals: [Int] = []
for node in path {

View File

@@ -42,7 +42,7 @@ const path: TreeNode[] = [];
const res: TreeNode[][] = [];
preOrder(root, path, res);
console.log('\n输出所有根节点到节点 7 的路径,路径中不包含值为 3 的节点,仅包含一个值为 7 的节点');
console.log('\n输出所有根节点到节点 7 的路径,路径中不包含值为 3 的节点');
res.forEach((path) => {
console.log(path.map((node) => node.val));
});

View File

@@ -43,7 +43,6 @@ function backtrack(
if (isSolution(state)) {
// 记录解
recordSolution(state, res);
return;
}
// 遍历所有选择
for (const choice of choices) {