Showing posts with label recursion. Show all posts
Showing posts with label recursion. Show all posts

Saturday, January 13, 2018

JavaScript: Largest smaller binary search tree key

January 13, 2018

Introduction


I do not have time to write a lot of JavaScript code last 12 months, since most of time I have to write C# code. But this morning 12:00 pm I had a mock interview, I met a programmer and then he showed me how to solve the problem using JavaScript.

Code review


Here is JavaScript code with his code from line 26 to 59. The peer came out the recursive solution and then base case correctly, and only comment I had is to clean up code, avoid duplicate node.right on line 46 and 51.

Since the peer solves the problem in 20 minutes, I told him that I will start my algorithm. After I complete the algorithm, I will ask him an extra algorithm problem.

Wednesday, January 3, 2018

Heap rank

January 3, 2018

Introduction



Plan to read the blog about heap rank, the link is here.

Here is the gist I created for the algorithm. It turns out that the algorithm is the great one to practice depth first search using recursive function.



I like to write a sentence to explain how to use recursive solution to solve the problem. Give the value x in the heap, start from root node and see if it is bigger than value x, if it is true, and also there is neither left or right child, then return 1; we handle the base case first, and then we ask left subtree and right subtree to solve the same problem as well.

Monday, June 15, 2015

Leetcode 226: invert a binary tree

Invert a binary tree
Binary tree upside down 
June 15, 2015
Write C# code 

/**
         * Latest update: on June 16, 2015
         * Leetcode: 
         * Invert a binary tree
         * Reference: 
         * http://www.zhihu.com/question/31202353
         * 
         * 7 lines of code - using recursion
         */
        public static Node invertBinaryTree(Node root)
        {
            if (root == null)
                return null;

            Node temp = root.left;
            root.left = root.right;
            root.right = temp;

            invertBinaryTree(root.left);
            invertBinaryTree(root.right);

            return root; 
        }

        /**
         * Latest update: on June 16, 2015
         * Leetcode: Invert a binary tree
         * using iterative solution 
         */
        public static Node invertBinaryTreeIterative(Node root)
        {
            if (root == null)
                return null;

            Queue q = new Queue();
            q.Enqueue(root);

            /*
             * consider the queue: 
             */
            while (q.Count > 0)
            {
                Node nd = (Node)q.Dequeue();

                Node tmp = nd.left;
                nd.left = nd.right;
                nd.right = tmp;

                if (nd.left != null)
                    q.Enqueue(nd.left);
                if (nd.right != null)
                    q.Enqueue(nd.right); 
            }

            return root; 
        }