递归,const的用法

递归
//void eatAppleXiaoMeng(int n)
//{
//    //对于递归来说,必须要有出口,也就是递归的结束标志,必须要有结束,否则就会有死循环
//    if (n == 0) {
//        return;//return后面什么都不写,代表返回值类型为空void
//    }
//    n--;//苹果个数减1
//    printf("晓檬吃第%d苹果\n", 4 - n);//吃了一个苹果,吃苹果
//    eatAppleXiaoMeng(n);//找自己吃苹果//找人
//}

求阶乘,5!
//int fac(int n)
//{
//    //出口
//    if (n == 1) {
//        return 1;//如果求1的阶乘直接返回1
//    }
//     //1 留1个数
//    int number = n;
//    //2 求得n-1的阶乘
//    int value = fac(n - 1);
//    //3 求n的阶乘
//    return number * value;



const
//const
//    //const 修饰变量,把变量当作常量,只能读,不能写
//    const int b = 10;
//    b = 7;
    //const修饰谁,谁就不可变
    int a = 10;
    int b = 11;
    const int *p = &a;
    //const修饰*p,不能通过指针变量p修改指向空间的内容,但是p可以重指向
    // p = &b;
    //*p = 18;
    int const *p1 = &a;
    //const修饰*p1,不能通过指针变量p1修改指向空间的内容,但是p可以重指向
    // p1 = &b;
    //*p1 = 18;
    int * const p2 = &a;
    //const修饰p2,不可以重指向,但是可以通过p2修改指向空间的内容
    // p2 = &b;
    //*p2 = 18;
    const int * const p3 = &a;
    //const修饰p3以及*p3,p3不可以重指向,也不可以通过*p3修改指向空间的内容
    //const  让程序安全
   // p3 = &b;
    //*p3 = 18;









  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 可以使用以下代码实现: function arrayToTree(arr, id = 'id', pid = 'parentId', children = 'children') { let map = {}; let res = []; for (let i = ; i < arr.length; i++) { map[arr[i][id]] = arr[i]; arr[i][children] = []; } for (let i = ; i < arr.length; i++) { let item = arr[i]; let parent = map[item[pid]]; if (parent) { parent[children].push(item); } else { res.push(item); } } return res; } ### 回答2: 数组转树的递归方法可以通过 JavaScript 来实现。具体步骤如下: 1. 创建一个空对象作为树的根节点。 2. 遍历数组中的每个元素,将每个元素插入到树中适当的位置。 3. 在插入元素时,首先需要根据元素的父节点找到对应的树节点。 4. 如果元素的父节点是根节点,则直接将元素插入到根节点的子节点数组中。 5. 如果元素的父节点不是根节点,则需要在当前树中递归查找该父节点,并将元素插入到父节点的子节点数组中。 6. 返回树的根节点。 下面是使用 JavaScript 实现数组转树递归方法的示例代码: ```javascript function arrayToTree(arr) { const tree = {}; // 创建树的根节点 arr.forEach(item => { const { id, parentId } = item; if (parentId === null || parentId === undefined) { // 如果元素的父节点是根节点,则直接插入到子节点数组中 if (!tree.children) { tree.children = []; } tree.children.push(item); } else { // 如果元素的父节点不是根节点,则递归查找父节点并插入到子节点数组中 findParentAndInsert(tree, item); } }); return tree; } function findParentAndInsert(node, item) { if (node.children) { const found = node.children.find(child => child.id === item.parentId); if (found) { if (!found.children) { found.children = []; } found.children.push(item); } else { node.children.forEach(child => findParentAndInsert(child, item)); } } } ``` 上述代码定义了一个 `arrayToTree` 的函数,用于将数组转换成树。传入的数组 `arr` 包含了要转换的元素,每个元素具有 `id` 和 `parentId` 属性,分别表示元素的唯一标识和父节点的标识。函数返回一个具有树结构的对象。 ### 回答3: JS写数组转树的递归方法可以按照以下步骤实现: 1. 创建一个空的树对象,用来存储转换后的树形结构。 2. 遍历数组,将每个元素添加到树中的适当位置。 3. 对于每个元素,将其根据指定的父子关系逐级添加到树中。 4. 如果一个元素的父级id为空或不存在,那么它就是树的根节点,将其添加到树对象中。 5. 如果一个元素的父级id存在于树中,则将其添加为该父级的子节点。 6. 重复上述步骤,直到遍历完所有元素。 7. 返回转换后的树对象。 以下是一个示例的JS代码,用于将数组转换为树的递归方法: ```javascript function arrayToTree(array) { const tree = {}; function addToTree(parentId, item) { if (!tree[parentId]) { tree[parentId] = {}; } tree[parentId][item.id] = item; if (item.children && item.children.length > 0) { item.children.forEach(child => { addToTree(item.id, child); }); } } array.forEach(item => { if (!item.parentId || !tree[item.parentId]) { tree[item.id] = item; } else { addToTree(item.parentId, item); } }); return tree; } // 示例用法 const array = [ { id: 1, parentId: null }, { id: 2, parentId: 1 }, { id: 3, parentId: 1 }, { id: 4, parentId: 2 }, { id: 5, parentId: 3 } ]; const tree = arrayToTree(array); console.log(tree); ``` 以上代码将会输出一个包含转换后树形结构的对象。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值