美文网首页
Java日记2018-05-26

Java日记2018-05-26

作者: hayes0420 | 来源:发表于2018-05-26 07:17 被阅读0次

晚上下班再补充

  1. 重建二叉树
    思路很简单,过程很绕,换句话说结果容易错

public class Solution0526 {
    static HashMap<Integer,Integer> arrl = new HashMap<>();
    public static TreeNode rebulid(int[] pre,int[] ino) {
        for(int i=0;i<ino.length;i++) {
            arrl.put(ino[i], i);
        }
        return rebulidcore(pre,0,pre.length-1,ino,0,ino.length-1);
    }
    public static TreeNode rebulidcore(int[] pre,int pleft,int pright,int[] ino,int ileft,int iright) {
        if(pleft>pright) return null;
        int index = arrl.get(pre[pleft]);
        int treesize = index-ileft;
        TreeNode root = new TreeNode(pre[pleft]);
        //记得给左右树赋值
        root.left = rebulidcore(pre,pleft+1,pleft+treesize,ino,ileft,ileft+treesize-1);
        root.right =rebulidcore(pre,pleft+treesize+1,pright,ino,ileft+treesize+1,iright);
        return root;
    }
    
    public static void main(String[] args) {
        int[] preorder = {3,9,20,15,7};
        int[] inorder = {9,3,15,20,7};
        TreeNode root2 = rebulid(preorder,inorder);
        System.out.println(root2.right.left);
    }

}

  1. 二叉树的下一个结点
    一直疑惑的是算法给的next节点是个什么鬼

① 如果一个节点的右子树不为空,那么该节点的下一个节点是右子树的最左节点;
② 否则,向上找第一个左链接指向的树包含该节点的祖先节点。

public class TreeLinkNode {
    int val;
    TreeLinkNode left = null;
    TreeLinkNode right = null;
    TreeLinkNode next = null;

    TreeLinkNode(int val) {
        this.val = val;
    }
}
public TreeLinkNode GetNext(TreeLinkNode pNode) {
    if (pNode.right != null) {
        TreeLinkNode node = pNode.right;
        while (node.left != null)
            node = node.left;
        return node;
    } else {
        while (pNode.next != null) {
            TreeLinkNode parent = pNode.next;
            if (parent.left == pNode)
                return parent;
            pNode = pNode.next;
        }
    }
    return null;
}
  1. 用两个栈实现队列
Stack<Integer> in = new Stack<Integer>();
Stack<Integer> out = new Stack<Integer>();

public void push(int node) {
    in.push(node);
}

public int pop() throws Exception {
    if (out.isEmpty())
        while (!in.isEmpty())
            out.push(in.pop());

    if (out.isEmpty())
        throw new Exception("queue is empty");

    return out.pop();
}

相关文章

  • Java日记2018-05-26

    晚上下班再补充 重建二叉树思路很简单,过程很绕,换句话说结果容易错 二叉树的下一个结点一直疑惑的是算法给的next...

  • 心晴5月28号觉察日记

    2018-05-26 18:10 · 字数 196 · 阅读 0 · 日记本 1.事件:今天再体验沙盘,没有进行完...

  • 2018-05-26

    2018-05-26· 字数 549· 阅读 86· 日记本 姓名:周富强 公司:厦门大科机械有限公司 日精进打卡...

  • 思然后知不足

    以为戒!——2018-05-26

  • 2018-05-26

    2018-05-26 戴师傅简书作者 2018-05-26 19:40 打开App (稻盛哲学学习会)打卡第6‘天...

  • 林子漫笔“微日记”(676-680):好心态前“无沼泽”

    【2018-05-26】谁是你的“钻石星” “Yes she likes me. Yes she wants me...

  • 2017-12-30

    JAVA学习日记(8) 多态!!很重要

  • 2017-12-29

    Java学习日记(4) 主要谈一下——继承extends 个 Tips : Java不像c++,Java是单继承(...

  • Java EE学习日记_JavaScript下

    layout: posttitle: Java EE学习日记_JavaScriptsubtitl...

  • 日精进打卡(第323天)

    2018-05-26 姓名:李义 公司:........ 组别:259期利他二组 【知~学习】 背诵 六项精进大纲...

网友评论

      本文标题:Java日记2018-05-26

      本文链接:https://www.haomeiwen.com/subject/ikbpjftx.html