面试经典150题(65-66)

leetcode 150道题 计划花两个月时候刷完,今天(第三十一天)完成了2道(65-66)150:

65.(106. 从中序与后序遍历序列构造二叉树)题目描述:

给定两个整数数组 inorder 和 postorder ,其中 inorder 是二叉树的中序遍历, postorder 是同一棵树的后序遍历,请你构造并返回这颗 二叉树 。

第一版(这个和昨天的给了中序和先序去构造二叉树差不多,先序《根左右》,后序《左右根》,还是递归)

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 buildSubTree(inorder,0,inorder.length-1,postorder,0,postorder.length-1);
    }
     public TreeNode buildSubTree(int[] inorder, int iStart,int iEnd,int[] postorder,int pStart,int pEnd) {
    
    
         if(iStart>iEnd||pStart>pEnd){
    
    
             return null;
         }
         if(pStart==pEnd){
    
    
             return new TreeNode(postorder[pEnd]);
         }
         TreeNode root=new TreeNode(postorder[pEnd]);
         int rootIndex=map.get(postorder[pEnd]);
         int count=rootIndex-iStart;
         root.left=buildSubTree(inorder,iStart,rootIndex-1,postorder,pStart,pStart+count-1);
         root.right=buildSubTree(inorder,rootIndex+1,iEnd,postorder,pStart+count,pEnd-1);
         return root;
    }
}

66.(117. 填充每个节点的下一个右侧节点指针 II)题目描述:

给定一个二叉树:
struct Node {
    
    
  int val;
  Node *left;
  Node *right;
  Node *next;
}
填充它的每个 next 指针,让这个指针指向其下一个右侧节点。如果找不到下一个右侧节点,则将 next 指针设置为 NULL 。
初始状态下,所有 next 指针都被设置为 NULL 。

第一版(一看到这题就想到了层次遍历,所以第一版就层次遍历去实现的,只需要把层次遍历框架先写出来,然后再加上一个当前层的前一个节点的变量记录就可以了)

class Solution {
    
    
    public Node connect(Node root) {
    
    
        if(root==null)
            return root;
        Queue<Node> queue=new LinkedList();
        queue.offer(root);
        while(!queue.isEmpty()){
    
    
           int currNum=queue.size();
           Node preNode=null;
           while(currNum!=0){
    
    
               Node temp=queue.poll();
               if(temp.left!=null){
    
    
                   if(preNode!=null){
    
    
                    preNode.next=temp.left;
                   }
                   preNode=temp.left;
                   queue.offer(temp.left);
               }
               if(temp.right!=null){
    
    
                   if(preNode!=null){
    
    
                       preNode.next=temp.right;  
                   }
                   preNode=temp.right;
                   queue.offer(temp.right);
               }
               currNum--;
           }
        }
        return root;
    }
}

第二版(看了解题,用上了题目中next指针,可以减少空间复杂度)


class Solution {
    
    
    public Node connect(Node root) {
    
    
        if(root==null){
    
    
            return root;
        }
        // 用 next 坐标串起来,先用上一层的把下一层的串好
        Node startNode=root;
        while(startNode!=null){
    
    
            Node preNode=null;
            // 记录下一层的 第一个节点
            Node lastNode=null;
            for(Node leftNode=startNode;leftNode!=null;leftNode=leftNode.next){
    
    
                if(leftNode.left!=null){
    
    
                    if(lastNode==null){
    
    
                        lastNode=leftNode.left;
                    }
                    if(preNode!=null)
                    {
    
    
                        preNode.next=leftNode.left;
                    }
                    preNode=leftNode.left;
                }
                if(leftNode.right!=null){
    
    
                    if(lastNode==null){
    
    
                        lastNode=leftNode.right;
                    }
                    if(preNode!=null)
                    {
    
    
                        preNode.next=leftNode.right;
                    }
                    preNode=leftNode.right;
                }
            }
            startNode=lastNode;
        }
        return root;

    }
}

这几天还在发懒中。。。不知道为啥。。还找了一个好看的动漫。。buff 拉满了。

第三十一天,加油,早日跳槽!!!

猜你喜欢

转载自blog.csdn.net/weixin_44061648/article/details/135397852