字节跳动2-1 三轮大数据方向算法20220330

Posted 史上最强的弟子

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了字节跳动2-1 三轮大数据方向算法20220330相关的知识,希望对你有一定的参考价值。

新鲜出炉,大数据的总监,一上来什么都没问,让我写一个非递归后续遍历。很不好意思让他打脸了,这个题我做过5片了,理解上还是很深刻的。我就想对他说为啥面试连自我介绍都不给我,就让我做题,这么无视我吗,我就想说我做出来了你咋个也不让我继续面了,太无理了。

package example;

import jdk.nashorn.internal.ir.BinaryNode;

import java.util.Stack;

public class asd 

    /**
     A
     B     E
     C
     D
     F    G
     * @param args
     */



    public static void main(String[] args) 
        ErChaNode erChaNodeA = new ErChaNode("A");
        ErChaNode erChaNodeB = new ErChaNode("B");
        ErChaNode erChaNodeC = new ErChaNode("C");
        ErChaNode erChaNodeD = new ErChaNode("D");
        ErChaNode erChaNodeE = new ErChaNode("E");
        ErChaNode erChaNodeF = new ErChaNode("F");
        ErChaNode erChaNodeG = new ErChaNode("G");
        erChaNodeA.leftNode = erChaNodeB;
        erChaNodeA.rightNode = erChaNodeE;

        erChaNodeB.leftNode = erChaNodeC;
        erChaNodeB.parentNode = erChaNodeA;

        erChaNodeC.rightNode = erChaNodeD;
        erChaNodeC.parentNode = erChaNodeB;

        erChaNodeD.leftNode = erChaNodeF;
        erChaNodeD.rightNode = erChaNodeG;
        erChaNodeD.parentNode = erChaNodeC;

        erChaNodeF.parentNode = erChaNodeD;
        erChaNodeG.parentNode = erChaNodeD;

        erChaNodeE.parentNode = erChaNodeA;

        //后序遍历
        new asd().endOutFor(erChaNodeA);
        System.out.println("");

        //后序遍历2
        new asd().endOutFor2(erChaNodeA);
        System.out.println("");
    

    //方法2:
    //后序遍历左右中
    private void endOutFor2(ErChaNode erChaNode) 

        ErChaNode root =erChaNode;
        ErChaNode zanCun = null;
        Stack<ErChaNode> stack = new Stack<>();
        while(root!=null||!stack.isEmpty()) 
            while (root != null) 
                stack.push(root);
                root = root.leftNode;
            
            root = stack.pop();
            if (root.rightNode == null || root.rightNode == zanCun) 
                System.out.print(root.value);
                zanCun = root;
                root = null;
             else 
                stack.push(root);
                root = root.rightNode;
            
        
    
    //方法1:后序遍历左右中
    private void endOutFor(ErChaNode erChaNode) 
        Stack<ErChaNode> stack1 = new Stack<>();
        Stack<Integer> stack2 = new Stack<>();
        int i = 1;
        while(erChaNode != null || !stack1.empty())
        
            while (erChaNode != null)
            
                stack1.push(erChaNode);
                stack2.push(0);
                erChaNode = erChaNode.leftNode;
            

            while(!stack1.empty() && stack2.peek() == i)
            
                stack2.pop();
                System.out.print(stack1.pop().value);
            

            if(!stack1.empty())
            
                stack2.pop();
                stack2.push(1);
                erChaNode = stack1.peek();
                erChaNode = erChaNode.rightNode;
            
        
    


class ErChaNode
    public ErChaNode parentNode;
    public ErChaNode leftNode;
    public ErChaNode rightNode;
    public String value;
    public  ErChaNode(String value)
        this.value = value;
    

输出结果:

以上是关于字节跳动2-1 三轮大数据方向算法20220330的主要内容,如果未能解决你的问题,请参考以下文章

字节跳动2-1 三轮大数据方向算法20220330

Golang开发面经字节跳动(三轮技术面)

Golang开发面经字节跳动(三轮技术面)

字节跳动八进八出,offer到手,发现项目不重要算法才最重要

二分查找技巧---字节跳动2018校招算法方向(第二批)---用户喜好

字节跳动2018校招算法方向(第一批) —— 1-最外层点