상량 공간은 두 갈래 나무를 체인 시계로 만든다

2732 단어
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.HashMap;

import javax.swing.tree.TreeNode;

public class Solution {



    static treeNode last=null;

    public static void main(String args[])
    {
        treeNode root=new treeNode();
        root.value=5;
        root.right=new treeNode();
        root.right.value=10;
        treeNode rootleft=new treeNode();
        rootleft.value=8;
        root.left=rootleft;

        treeNode left=new treeNode();
        left.value=4;

        treeNode right=new treeNode();
        right.value=9;

        rootleft.left=left;
        rootleft.right=right;



        fun(root);
        treeNode record=last;
        while(last!=null)
        {
            System.out.println(last.value);
            last=last.left;
        }
        last=record;
        while(last.left!=null)
        {
            last.left.right=last;
            last=last.left;
        }

        while(last!=null)
        {
            System.out.println(last.value);
            last=last.right;
        }
    }

    static void fun(treeNode root)
    {
        if(root.left!=null)
        {
            fun(root.left);
        }
        root.left=last;
        last=root;
        if(root.right!=null)
        {
            fun(root.right);
        }

    }


}

class treeNode
{
    int value;
    treeNode left=null;
    treeNode right=null;
}

좋은 웹페이지 즐겨찾기