二叉树找到和为n的所有路径

 //本题中的路径指的是从根结点到叶节点
      var tree={
        root:1,
        rchild:{
          root:2,
          rchild:null,
          lchild:{
            root:1,
            rchild:null,
            lchild:null,
          }
        },
        lchild:{
          root:3,
          rchild:null,
          lchild:null
        }
      }
      function findPath(tree,Num){
        var result = []
        if(tree.root===null){
          return result
        }

        dnsfind(tree,Num,0,[],result)
        return result
      }

      function dnsfind(tree,Num,currentSum,path,result){
        currentSum+=tree.root
        path.push(tree.root)
        //当叶节点满足要求
        if(currentSum===Num&&tree.rchild===null&&tree.lchild===null){
          result.push(path.slice(0)) 
          //因为path是引用类型,遍历结束后,path会pop会根结点,也就是变为[],如果这里直接存储path的话,最后也会变成[]
        }
        //前序遍历
        if(tree.lchild!==null){
          dnsfind(tree.lchild,Num,currentSum,path,result)
        }
        if(tree.rchild!==null){
          dnsfind(tree.rchild,Num,currentSum,path,result)
        }
        //当到达叶节点但却不满足要求时,pop会到上一节点,继续遍历
        path.pop()
      }
      console.log(findPath(tree,4)) //[[1,3],[1,2,1]]
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现这个功能需要对二叉树进行遍历,找到最长路径上的所有节点。以下是一个基于递归的实现: ```c #include <stdio.h> #include <stdlib.h> // 二叉树结构体 typedef struct TreeNode { int val; struct TreeNode* left; struct TreeNode* right; } TreeNode; // 计算树的高度 int getHeight(TreeNode* root) { if (root == NULL) { return 0; } int leftHeight = getHeight(root->left); int rightHeight = getHeight(root->right); return (leftHeight > rightHeight ? leftHeight : rightHeight) + 1; } // 打印从根节点叶子节点路径 void printPath(int path[], int len) { for (int i = 0; i < len; i++) { printf("%d ", path[i]); } printf("\n"); } // 在树中查找最长路径 void findLongestPath(TreeNode* root, int path[], int len) { if (root == NULL) { return; } path[len++] = root->val; if (root->left == NULL && root->right == NULL) { printPath(path, len); } else { findLongestPath(root->left, path, len); findLongestPath(root->right, path, len); } } int main() { // 构造一个二叉树 TreeNode* root = (TreeNode*)malloc(sizeof(TreeNode)); root->val = 1; root->left = (TreeNode*)malloc(sizeof(TreeNode)); root->left->val = 2; root->right = (TreeNode*)malloc(sizeof(TreeNode)); root->right->val = 3; root->left->left = (TreeNode*)malloc(sizeof(TreeNode)); root->left->left->val = 4; root->left->right = (TreeNode*)malloc(sizeof(TreeNode)); root->left->right->val = 5; root->right->left = (TreeNode*)malloc(sizeof(TreeNode)); root->right->left->val = 6; root->right->right = (TreeNode*)malloc(sizeof(TreeNode)); root->right->right->val = 7; root->left->left->left = (TreeNode*)malloc(sizeof(TreeNode)); root->left->left->left->val = 8; // 计算树的高度 int height = getHeight(root); // 分配存放路径的数组 int* path = (int*)malloc(height * sizeof(int)); // 查找最长路径 findLongestPath(root, path, 0); // 释放内存 free(path); free(root->left->left->left); free(root->right->right); free(root->right->left); free(root->left->right); free(root->left->left); free(root->right); free(root->left); free(root); return 0; } ``` 输出结果为: ``` 1 2 4 8 1 2 5 1 3 6 1 3 7 ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值