Tag: obj problem does not loop append inf back using Val
1. Description of the problem
Leetcode No. 226, title two fork Tree Flip
Triv I a:details, considerations, or pieces of information of little importance or value. Translate into details, useless details
2. Thinking
# Definition for a binary tree node.# class TreeNode (object): # def __init__ (self, x): # self.val = x# self.left = none# self.right = Noneclass Solution (object): def inverttree (self, Root): "" " : Type root:treenode< C7/>:rtype:treenode "" "
2.1 For this class to pass in a root, first determine if root is None
2.2 Flip the root node to root for the two fork tree, the problem is broken down to flip the root node is root.left two fork tree, flip the root node is root.right two fork tree, swap root node left and right subtree.
Flipping the root node to root two-tree = flips the root node to the Root.left two-tree (self-similarity of the problem, using recursive recursive resolution)
+ Flip the root node to Root.right's two-fork tree (self-similarity of the problem, using recursive resolution)
+ Swap root node left and right subtree
2.3 Thinking about recursive end conditions
A root node left and right subtree does not exist, flipping the node does not make sense, directly return to the node can be
Or
The root node itself does not exist (this is the case described in 2.1), and the direct return to none is possible.
3. Problem Solution
#Definition for a binary tree node.#class TreeNode (object):#def __init__ (self, x):#self.val = x#self.left = None#self.right = Noneclasssolution (object):defInverttree (self, root):""": Type Root:TreeNode:rtype:TreeNode""" if notRoot:returnNoneifRoot.leftorRoot.right:root.left, Root.right=Self.inverttree (root.right), Self.inverttree (Root.left)returnRoot
3.1 Refactoring Code
def inverttree (self, root): #就假设root存在 If the segment code does not exist by default returns none if root: root.left, root.right = Self.inverttree (Root.right), Self.inverttree (Root.left) return root
3.2 Build a stack, simulate the system maintenance call stack,
This is actually DFS, this DFS order is from the tree's right sub-tree to continue to go deep, but because the tree's left and the sub-tree exchange position, so in the visualization inside looks like always from the tree's left subtree constantly in depth
# Dfsdef Inverttree (self, root): #stack初始化 stack = [Root] while stack: #循环做如下动作: Pop Deletes the element at the end of the list, doing the operation, The new element is also added to the list at the end of node = Stack.pop () if node: node.left, node.right = node.right, node.left stack + = Node.left, Node.right #向stack中加入待处理节点 return root
3.3 Build yourself a queue,
Can implement BFS, first append left dial hand tree, and then append right subtree, so the entire traversal process is from left to right layer by step of the traversal
# Bfsdef invertTree2 (self, root): queue = Collections.deque ([root]) while queue: node = Queue.popleft () C3/>if node: node.left, node.right = node.right, Node.left queue.append (node.left) #向queue中加入待处理节点 Queue.append (Node.right) return root
4. Visualization of code
4.1 DFS
4 / 2 7 stack = [4] the node in the stack that needs to be flipped is stored/\ /1 3 6 9
4 / 7 2 stack = [2, 7]/\ /6 9 1 3
4 / 7 2 stack = [2, 6, 9]/\ /9 6 1 3
Such a structure accurately describes the process of flipping, the following is not detailed drawing
5. Reference links
Refactoring code in 3, build a stack for yourself from Leetcode discuss
The BFs method in 3 is derived from Leetcode discuss
Invert Binary Tree---Python---leetcode