mirror of
https://github.com/krahets/hello-algo.git
synced 2025-12-16 03:59:18 +08:00
Update preorder_traversal_iii.
This commit is contained in:
@@ -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) {
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
|
||||
@@ -38,7 +38,6 @@ public class preorder_traversal_iii_template {
|
||||
if (isSolution(state)) {
|
||||
// 记录解
|
||||
recordSolution(state, res);
|
||||
return;
|
||||
}
|
||||
// 遍历所有选择
|
||||
foreach (TreeNode choice in choices) {
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -41,7 +41,6 @@ public class preorder_traversal_iii_template {
|
||||
if (isSolution(state)) {
|
||||
// 记录解
|
||||
recordSolution(state, res);
|
||||
return;
|
||||
}
|
||||
// 遍历所有选择
|
||||
for (TreeNode choice : choices) {
|
||||
|
||||
@@ -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));
|
||||
});
|
||||
|
||||
@@ -38,7 +38,6 @@ function backtrack(state, choices, res) {
|
||||
if (isSolution(state)) {
|
||||
// 记录解
|
||||
recordSolution(state, res);
|
||||
return;
|
||||
}
|
||||
// 遍历所有选择
|
||||
for (const choice of choices) {
|
||||
|
||||
@@ -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])
|
||||
|
||||
@@ -43,7 +43,6 @@ def backtrack(
|
||||
if is_solution(state):
|
||||
# 记录解
|
||||
record_solution(state, res)
|
||||
return
|
||||
# 遍历所有选择
|
||||
for choice in choices:
|
||||
# 剪枝:检查选择是否合法
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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));
|
||||
});
|
||||
|
||||
@@ -43,7 +43,6 @@ function backtrack(
|
||||
if (isSolution(state)) {
|
||||
// 记录解
|
||||
recordSolution(state, res);
|
||||
return;
|
||||
}
|
||||
// 遍历所有选择
|
||||
for (const choice of choices) {
|
||||
|
||||
Reference in New Issue
Block a user