Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            //PowerTreeNode root = new PowerTreeNode(1);
            //root.left = new PowerTreeNode(2);
            //root.right = new PowerTreeNode(3);
            //root.left.left = new PowerTreeNode(4);
            //root.right.left = new PowerTreeNode(5);
            //root.right.right = new PowerTreeNode(6);
            //root.left.left.left = new PowerTreeNode(7);


            LinkTreeNode linkRoot = new LinkTreeNode(1);
            linkRoot.left = new LinkTreeNode(2);
            linkRoot.right = new LinkTreeNode(3);
            linkRoot.left.left = new LinkTreeNode(4);
            linkRoot.right.left = new LinkTreeNode(5);
            linkRoot.right.right = new LinkTreeNode(6);
            linkRoot.left.left.left = new LinkTreeNode(7);
            linkRoot.right.right.left = new LinkTreeNode(8);

            Console.WriteLine(String.Join(" ", ZigZagTraversal.TraverseSimple(linkRoot)));


            //TreeNode simpleRoot = new TreeNode(1);
            //simpleRoot.left = new TreeNode(2);
            //simpleRoot.right = new TreeNode(3);
            //simpleRoot.left.left = new TreeNode(4);
            //simpleRoot.right.left = new TreeNode(5);
            //simpleRoot.right.right = new TreeNode(6);
            //simpleRoot.left.left.left = new TreeNode(7);

            //Console.WriteLine(String.Join(" ", ZigZagTraversal.TraverseWithSimpleNode(simpleRoot)));

            //int[] cards = new int[54];
            //for (int i = 0; i < 54; i++) {
            //    cards[i] = i;
            //}

            //ShuffleCard.Go(cards);
            //Console.WriteLine(String.Join(" ", cards));

            Console.ReadLine();
        }
Ejemplo n.º 2
0
        // #2 in 1/29 discussion

        // O(1) space
        // change structure of node with link only in one direction
        public static List<int> TraverseSimple(LinkTreeNode root) { 
            if (root == null) return new List<int>();

            List<int> res = new List<int>();
            LinkTreeNode curr = root;
            LinkTreeNode head = null; // head node in next level
            bool rightToLeft = true;    // direction, view first level as from right to left

            // two children of current node waiting for linking
            LinkTreeNode p = null;
            LinkTreeNode q = null;

            while (curr != null) {
                res.Add(curr.val);
                p = rightToLeft ? curr.right : curr.left;
                q = rightToLeft ? curr.left : curr.right;
                
                // link children
                if (p != null) {
                    if(head != null) {
                        p.next = head;
                    }
                    head = p;
                }
                if (q != null){
                    if (head != null) {
                        q.next = head;
                    }
                    head = q;
                }
                // move to next level
                if (curr.next == null) {
                    curr.next = head;
                    head = null;
                    rightToLeft = !rightToLeft;
                }

                curr = curr.next;
            }

            return res;
        }