怎么用数组表示二叉树

本文介绍了如何使用数组表示二叉树,并通过实例展示了在无限二叉树的‘之’字形结构中,如何利用DFS算法寻找从根节点到指定节点的路径。重点讲解了Solution类中的pathInZigZagTree方法和dfs辅助函数。

怎么用数组表示二叉树

数组怎么表示二叉树

如果父节点的下标是i那么左子节点的下标i*2+1右子节点的下标的i*2+2

1104. 二叉树寻路

难度中等99

在一棵无限的二叉树上,每个节点都有两个子节点,树中的节点 逐行 依次按 “之” 字形进行标记。

如下图所示,在奇数行(即,第一行、第三行、第五行……)中,按从左到右的顺序进行标记;

而偶数行(即,第二行、第四行、第六行……)中,按从右到左的顺序进行标记。

img

给你树上某一个节点的标号 label,请你返回从根节点到该标号为 label 节点的路径,该路径是由途经的节点标号所组成的。

示例 1:

输入:label = 14
输出:[1,3,4,14]

示例 2:

输入:label = 26
输出:[1,2,6,10,26]
class Solution {
public:
    vector<int> res;
    vector<int> pathInZigZagTree(int label) {
        // 首先你要确定这个数组的大小
        int value = 1;
        int up = 2;
        while(value<label) {
            value+=up;
            up=up*2;
        }
        vector<int> data(value);
        // 开始进行赋值
        value=1;
        bool left2righ=true;
        int num = 1;
        int index = 0;
        while(index<data.size()) {
            int end_value=value+num;
            if(left2righ) {
                for(int i=0;i<num;i++) {
                    data[index++]=value;
                    value++;
                }
                left2righ=false;
            } else {
                int tmp=end_value-1;
                for(int i=0;i<num;i++) {
                    data[index++]=tmp;
                    tmp--;
                }
                left2righ=true;
            }
            num*=2;
            value=end_value;
        }

        // for(auto a : data) {
        //     cout<<a<<" ";
        // }
        // 现在开始寻找
        vector<int> path;
        dfs(data,label,path,0);

       return res;

    }

    void dfs(vector<int>& data , int target, vector<int>& path,int index) {
        // 开始回溯
        // for(auto a : path) {
        //     cout<<a<<" ";
        // }
        // cout<<endl;
      
          if(!path.empty() && path.back()==target) {
            res=path;
            return;
        }
        if(index>=data.size()) {
            return;
        }
     
        // 按照中左右遍历
        path.push_back(data[index]);
        dfs(data,target,path,index*2+1);
        // path.pop_back();

        // path.push_back(data[index]);
        dfs(data,target,path,index*2+2);
        path.pop_back();

    }

};

也可以根据下标反向递推

class Solution {
public:
    vector<int> res;
    vector<int> pathInZigZagTree(int label) {
        // 首先你要确定这个数组的大小
        int value = 1;
        int up = 2;
        while(value<label) {
            value+=up;
            up=up*2;
        }
        vector<int> data(value);
        // 开始进行赋值
        value=1;
        bool left2righ=true;
        int num = 1;
        int index = 0;
        int target_index =0;
        while(index<data.size()) {
            int end_value=value+num;
            if(left2righ) {
                for(int i=0;i<num;i++) {
                    if(value==label) {
                        target_index=index;
                    }
                    data[index++]=value;
                    value++;
                }
                left2righ=false;
            } else {
                int tmp=end_value-1;
                for(int i=0;i<num;i++) {
                    if(tmp==label) {
                        target_index=index;
                    } 
                    data[index++]=tmp;
                    tmp--;
                }
                left2righ=true;
            }
            num*=2;
            value=end_value;
        }

        // for(auto a : data) {
        //     cout<<a<<" ";
        // }
        // 现在开始寻找
        // vector<int> path;
        // dfs(data,label,path,0);

        // 这时候可以不用递归,可以直接反过来寻找路径
        while(target_index>=0) {
            if((target_index-1)%2==0) {
                res.push_back(data[target_index]);
                target_index=(target_index-1)/2;
            } else {
                res.push_back(data[target_index]);
                target_index=(target_index-2)/2;
            }
        }
        reverse(res.begin(),res.end());
        
       return res;

    }

    void dfs(vector<int>& data , int target, vector<int>& path,int index) {
        // 开始回溯
          if(!path.empty() && path.back()==target) {
            res=path;
            return;
        }
        if(index>=data.size()) {
            return;
        }
        // for(auto a : path) {
        //     cout<<a<<" ";
        // }
        // cout<<endl;
      
        // 按照中左右遍历
        path.push_back(data[index]);
        dfs(data,target,path,index*2+1);
        path.pop_back();

        path.push_back(data[index]);
        dfs(data,target,path,index*2+2);
        path.pop_back();

    }

};
```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值