Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

morris Inorder Traversal python

class Solution(object):
def inorderTraversal(self, current):
    soln = []
    while(current is not None):    #This Means we have reached Right Most Node i.e end of LDR traversal

        if(current.left is not None):  #If Left Exists traverse Left First
            pre = current.left   #Goal is to find the node which will be just before the current node i.e predecessor of current node, let's say current is D in LDR goal is to find L here
            while(pre.right is not None and pre.right != current ): #Find predecesor here
                pre = pre.right
            if(pre.right is None):  #In this case predecessor is found , now link this predecessor to current so that there is a path and current is not lost
                pre.right = current
                current = current.left
            else:                   #This means we have traverse all nodes left to current so in LDR traversal of L is done
                soln.append(current.val) 
                pre.right = None       #Remove the link tree restored to original here 
                current = current.right
        else:               #In LDR  LD traversal is done move to R  
            soln.append(current.val)
            current = current.right

    return soln
Comment

morris Inorder Traversal python

  public class TreeNode
    {
        public int val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(int val = 0, TreeNode left = null, TreeNode right = null)
        {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    class MorrisTraversal
    {
        public static IList<int> InOrderTraversal(TreeNode root)
        {
            IList<int> list = new List<int>();
            var current = root;
            while (current != null)
            {
                //When there exist no left subtree
                if (current.left == null)
                {
                    list.Add(current.val);
                    current = current.right;
                }
                else
                {
                    //Get Inorder Predecessor
                    //In Order Predecessor is the node which will be printed before
                    //the current node when the tree is printed in inorder.
                    //Example:- {1,2,3,4} is inorder of the tree so inorder predecessor of 2 is node having value 1
                    var inOrderPredecessorNode = GetInorderPredecessor(current);
                    //If the current Predeccessor right is the current node it means is already printed.
                    //So we need to break the thread.
                    if (inOrderPredecessorNode.right != current)
                    {
                        inOrderPredecessorNode.right = null;
                        list.Add(current.val);
                        current = current.right;
                    }//Creating thread of the current node with in order predecessor.
                    else
                    {
                        inOrderPredecessorNode.right = current;
                        current = current.left;
                    }
                }
            }

            return list;
        }

        private static TreeNode GetInorderPredecessor(TreeNode current)
        {
            var inOrderPredecessorNode = current.left;
            //Finding Extreme right node of the left subtree
            //inOrderPredecessorNode.right != current check is added to detect loop
            while (inOrderPredecessorNode.right != null && inOrderPredecessorNode.right != current)
            {
                inOrderPredecessorNode = inOrderPredecessorNode.right;
            }

            return inOrderPredecessorNode;
        }
    }
Comment

PREVIOUS NEXT
Code Example
Python :: pylatex add section without numbering 
Python :: explorer gives new window 
Python :: discord.py delete own message 
Python :: how to get user id discord.py 
Python :: ModelCheckpoint 
Python :: concatenate dataframes using one column 
Python :: python selenium login button class click 
Python :: np.nditer 
Python :: Applies a function to all elements of this RDD. 
Python :: visualizzare csv in pycharm pandas read_csv 
Python :: there is no difference in R between a string scalar and a vector of strings 
Python :: calling a function in python upon entry content changing tkinter 
Python :: couple legend from twin axes python 
Python :: django rest framework encrypt passwors 
Python :: install iris 
Python :: set list start at 1 python 
Python :: if query empty print python 
Python :: matplotlib text relative to axis 
Python :: iterate through keys in dictionary 
Python :: urllib.error.HTTPError: HTTP Error 502 docker redis 
Python :: how to type shashank in python 
Python :: how to pull images from android device from usb in python 
Python :: Display all resources in table pandas 
Python :: Feature Importance 
Python :: zbarge / s3gui 
Python :: check cudann 
Python :: change order of headers pandas 
Python :: python plot outline imdbpy 
Python :: python code for diamond with gap between odd rows 
Python :: python filter function using lambda function as one of the parameters 
ADD CONTENT
Topic
Content
Source link
Name
8+4 =