01.17

106.从中序与后序遍历序列构造二叉树

思路

按照正常思路走,找到中序序列中在后序序列最靠近尾部的值作为根节点,在中序序列中以该值为中心分出两个序列,分别为左子树与右子树的节点,不断按照上述方法递归,若传入序列大小为1则返回。

总结

按照上述思路,写出来代码,很是辣鸡。题解的递归方法十分巧妙,确定好后序序列的值后(后序序列最后一个值,每次递归往前推一位,所以后序序列不断缩小,因为是后序遍历,从后往前是“中右左”,所以它的值最前面的为左树的值,因此优先遍历右子树,不断向左子树遍历,),传入的是子树的头尾两个指针(中序序列首尾指针),根据指针来保存一个子树的所有节点值。

迭代看不懂

代码

正常思路(辣鸡)

class Solution {
    HashMap<Integer,Integer> map=new HashMap<>();

    public TreeNode buildTree(int[] inorder, int[] postorder) {

        for (int i=0;i< postorder.length;i++){
            map.put(postorder[i],i);
        }
        TreeNode root=new TreeNode();
        root.val=postorder[postorder.length-1];
        int size= inorder.length;
        build(root,inorder,postorder,size);
        return root;
    }
    public void build(TreeNode root,int[] inorder,int[] postorder,int size){
        //若inorder只包含一个节点,则为叶子节点,退出
        if (size<2) return;

        int[] left=new int[inorder.length];
        int[] right=new int[inorder.length];

        boolean f=false;
        int l_size=0,r_size=0;
        for (int j=0;j< size;j++){
            if (inorder[j]==root.val) {
                f=true;
                continue;
            }
            if (f) {
                right[r_size++]=inorder[j];
            } else {
                left[l_size++]=inorder[j];
            }
        }
        //找到后续数组中最右的值下标作为子树
        int l_max=-1,r_max=-1;
        for (int j=0;j<l_size;j++){
            if (l_max<map.get(left[j])) l_max=map.get(left[j]);
        }
        for (int j=0;j<r_size;j++){
            if (r_max<map.get(right[j])) r_max=map.get(right[j]);
        }
        TreeNode leftNode=new TreeNode();
        TreeNode rightNode=new TreeNode();

        if (l_max!=-1) {
            leftNode.val=postorder[l_max];
        }else leftNode=null;
        if (r_max!=-1) {
            rightNode.val=postorder[r_max];
        }else rightNode=null;
        root.left=leftNode;
        root.right=rightNode;

        build(leftNode,left,postorder,l_size);
        build(rightNode,right,postorder,r_size);

    }

}

双指针思路

class Solution {
    int post_idx;
    int[] postorder;
    int[] inorder;
    Map<Integer, Integer> idx_map = new HashMap<Integer, Integer>();

    public TreeNode helper(int in_left, int in_right) {
        // 如果这里没有节点构造二叉树了,就结束
        if (in_left > in_right) {
            return null;
        }

        // 选择 post_idx 位置的元素作为当前子树根节点
        int root_val = postorder[post_idx];
        TreeNode root = new TreeNode(root_val);

        // 根据 root 所在位置分成左右两棵子树
        int index = idx_map.get(root_val);

        // 下标减一
        post_idx--;
        // 构造右子树
        root.right = helper(index + 1, in_right);
        // 构造左子树
        root.left = helper(in_left, index - 1);
        return root;
    }

    public TreeNode buildTree(int[] inorder, int[] postorder) {
        this.postorder = postorder;
        this.inorder = inorder;
        // 从后序遍历的最后一个元素开始
        post_idx = postorder.length - 1;

        // 建立(元素,下标)键值对的哈希表
        int idx = 0;
        for (Integer val : inorder) {
            idx_map.put(val, idx++);
        }
        
        return helper(0, inorder.length - 1);
    }
}

  • 14
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当你想要查询Linux系统CPU状态时,可以使用top命令。Top命令可以显示系统当前的进程信息以及资源占用情况,其中包括CPU占用情况。 以下是一个简单的脚本,使用top命令查询CPU状态并输出到终端: ``` #!/bin/bash while true do clear top -b -n 1 | head -n 20 sleep 1 done ``` 这个脚本会循环执行top命令,每秒钟更新一次CPU状态信息。执行脚本后,你可以在终端上看到类似下面的输出: ``` top - 12:45:15 up 5 min, 1 user, load average: 0.74, 0.47, 0.24 Tasks: 211 total, 1 running, 210 sleeping, 0 stopped, 0 zombie %Cpu(s): 25.3 us, 4.3 sy, 0.0 ni, 70.3 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st KiB Mem : 2048000 total, 151664 free, 272236 used, 1629100 buff/cache KiB Swap: 1048572 total, 1048572 free, 0 used. 1496540 avail Mem PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 4054 user 20 0 112548 5764 3208 R 16.7 0.3 0:00.02 top 1 root 20 0 225280 6356 3876 S 0.0 0.3 0:01.17 systemd 2 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kthreadd 3 root 20 0 0 0 0 S 0.0 0.0 0:00.02 ksoftirqd/0 5 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kworker/0:+ 7 root RT 0 0 0 0 S 0.0 0.0 0:00.00 migration/0 8 root RT 0 0 0 0 S 0.0 0.0 0:00.00 rcu_bh 9 root RT 0 0 0 0 S 0.0 0.0 0:00.00 rcu_sched 10 root 20 0 0 0 0 S 0.0 0.0 0:00.00 lru-add-dr+ 11 root 20 0 0 0 0 S 0.0 0.0 0:00.00 watchdog/0 12 root 20 0 0 0 0 S 0.0 0.0 0:00.00 cpuhp/0 13 root 20 0 0 0 0 S 0.0 0.0 0:00.00 cpuhp/1 14 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kdevtmpfs 15 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 netns ``` 其中,%Cpu(s)行显示了CPU的占用情况,包括用户态进程占用时间(us)、系统态进程占用时间(sy)、空闲时间(id)等。PID、COMMAND等列则显示了当前正在运行的进程信息。 你可以按q键退出top命令并停止脚本的执行。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值