7树

3构建

就是分治 找准位置就行 前后有个n+1所以需要判断

前中

class Solution {
    //分治 
    //语义是 [a,b]那么就要根据这个来
    Map<Integer,Integer> map = new HashMap<>();
    public TreeNode buildTree(int[] pre, int[] mid) {
        //把逻辑捋清楚再说
        for(int i = 0; i<mid.length; i++){
            //我们是想要下标 所以这里v是i
            map.put(mid[i],i);
        }
        return build(pre,0,pre.length-1,0,pre.length-1);
    }
    //分治一定是相对位置
    private TreeNode build(int[] pre, int p_a, int p_b, int m_a, int m_b){
        //我们只用pre
        if(p_a > p_b || m_a > m_b) return null;
        //p不需要mid
        //int p_mid = (p_a + p_b) / 2;
        int m_mid = map.get(pre[p_a]);
        TreeNode root = new TreeNode(pre[p_a]);
        //要把左半部分长度求出来
        int lenl = m_mid - m_a;
        int lenr = m_b - m_mid;
        root.left = build(pre,p_a+1,p_a+lenl,m_a,m_mid-1);
        root.right = build(pre,p_a+1+lenl,p_b,m_mid+1,m_b);
        return root;
    }
}

中后

class Solution {
    Map<Integer,Integer> map = new HashMap<>();
    public TreeNode buildTree(int[] inorder, int[] postorder) {
        for(int i =0; i < inorder.length; i++){
            map.put(inorder[i],i);
        }
        return build(postorder,0,inorder.length-1,0,inorder.length-1);
    }
    private TreeNode build(int[] post,int m_a, int m_b, int h_a, int h_b){
        if(m_a > m_b || h_a > h_b) return null;
        int tar = post[h_b];
        int mid = map.get(tar);
        //找长度 不难 
        int len = mid-m_a;
        TreeNode root = new TreeNode(tar);
        root.left = build(post,m_a,mid-1,h_a,h_a+len-1);
        root.right =build(post,mid+1,m_b,h_a+len,h_b-1) ;
        return root;
    }
}

前后

class Solution {
    Map<Integer,Integer> map = new HashMap<>();
    public TreeNode constructFromPrePost(int[] preorder, int[] postorder) {
        for(int i = 0 ;i<postorder.length;i++){
            map.put(postorder[i],i);
        }
        return build(preorder,0,postorder.length-1,0,postorder.length-1);
    }
    //按常规的写一写
    private TreeNode build(int[] pre,int p_a,int p_b,int w_a,int w_b){
        if(p_a > p_b || w_a > w_b) return null;
        //按index来
        TreeNode root = new TreeNode(pre[p_a]);
        // if(p_a+1 > p_b) return root;
        int index = map.get(pre[p_a+1]);
        int len = index - w_a+1;
        root.left = build(pre, p_a+1, p_a+len, w_a, index);
        root.right = build(pre, p_a+len+1, p_b, index+1, w_b-1);
        return root; 
    }
}

3遍历

前太简单。和递归一样,只能从stack拿。来源有两个,stack和root。后可能需要回炉,判断条件就是pre。

class Solution {
    public List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> res = new ArrayList<>();
        if(root == null) return res;
        Deque<TreeNode> stack = new LinkedList<>();
        //用普通的就好
        stack.push(root);
        while(!stack.isEmpty()){
            TreeNode t = stack.pop();
            res.add(t.val);
            if(t.right != null) stack.push(t.right);
            if(t.left != null) stack.push(t.left);
        }
        return res;
    }
}

class Solution {
    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> res = new ArrayList<>();
        Deque<TreeNode> stack = new LinkedList<>();
        //需要用到stack或者root
        while(!stack.isEmpty() || root != null){
            while(root != null){
                stack.push(root);
                root = root.left;
            }
            TreeNode t = stack.pop();
            res.add(t.val);
            root = t.right;
        }
        return res;
    }
}

class Solution {
    public List<Integer> postorderTraversal(TreeNode root) {
        List<Integer> res = new ArrayList<>();
        Deque<TreeNode> stack = new LinkedList<>();
        TreeNode pre = null;
        while(!stack.isEmpty() || root != null){
            while(root != null){
                stack.push(root);
                root = root.left;
            }
            TreeNode t = stack.pop();
            if(t.right == null || pre == t.right){
                res.add(t.val);
                pre = t;
                root = null;
            }else{
                stack.push(t);
                root = t.right;
            }
        }
        return res;
    }
}

1层次

size是队列长度就好了

class Solution {
    public List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> res = new ArrayList<>();
        if(root == null) return res;
        //List<List<Integer>> tmp = new ArrayList<>();
        //无非就是一个进队 然后丢失
        Deque<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        //
        while(!queue.isEmpty()){
            List<Integer> cur = new ArrayList<>();
            int x = queue.size();
            for(int i = 0 ; i<x; i++){
                TreeNode t = queue.poll();
                cur.add(t.val);
                if(t.left != null) queue.offer(t.left);
                if(t.right != null) queue.offer(t.right);
            }
            res.add(cur);
        }
        return res;
    }
}
posted @ 2021-09-19 20:23  常熟阿诺  阅读(108)  评论(0编辑  收藏  举报