preorder and then inorder


  • 0
    S
    # Definition for binary tree with next pointer.
    # class TreeLinkNode:
    #     def __init__(self, x):
    #         self.val = x
    #         self.left = None
    #         self.right = None
    #         self.next = None
    
    class Solution:
        # @param root, a tree link node
        # @return nothing
        def _preorder(self,root,parent):
            if root:
                print(root.val)
                root.next = parent
                self._preorder(root.left,root)
                self._preorder(root.right,root)
    
        def _inorder(self,root,parent):
            if root:
                self._inorder(root.left,root)
    
                if parent:
                    if root is parent.left:
                        root.next = parent.right
                    else:
                        root.next = parent.next.left if parent.next else None
                        
                self._inorder(root.right,root)
                    
        def connect(self, root):
            self._preorder(root,None)
            self._inorder(root,None)

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.