introduction:
the difficulty of collecting questions is at the simple level and intermediate level. It is also a common topic in interviews. The development language used for the solution of the problem is Swift.
because the description of the topic is very long and there are various case tips, it is not displayed in order to not occupy space. You can query directly through the topic number or click the link to view the description of the topic.
the writing order of the article is: 1 Show the title number and the link to the title 2 Narration of core ideas 3 Code implementation.
Finally, the code provided in this article is submitted on LeetCode.
Binary Tree Questions
1.LeetCode_144: preorder traversal of binary tree
2.LeetCode_102: sequence traversal of binary tree
3.LeetCode_104: maximum depth of binary tree
4.LeetCode_226: flip binary tree
5.LeetCode_110: judge whether a binary tree is a balanced binary tree
6.LeetCode_101: symmetric binary tree
7.LeetCode_100: same tree
1. Preorder traversal of binary tree
1.1 core idea:
It is very simple to traverse before, during and after binary tree
The sequence of preorder traversal is: root, left and right.
Recursive implementation, so first access the root node, then access the left subtree, and then access the right subtree
1.2 code implementation:
func preorderTraversal(_ root: TreeNode?) -> [Int] { guard let root = root else { return [] } nums.append(root.val) preorderTraversal(root.left) preorderTraversal(root.right) return nums }
The middle order traversal should be written as follows:
func preorderTraversal(_ root: TreeNode?) -> [Int] { guard let root = root else { return [] } preorderTraversal(root.left) nums.append(root.val) preorderTraversal(root.right) return nums }
Post order traversal should be written as follows:
func preorderTraversal(_ root: TreeNode?) -> [Int] { guard let root = root else { return [] } preorderTraversal(root.left) preorderTraversal(root.right) nums.append(root.val) return nums }
2. Sequence traversal of binary tree
2.1 core idea:
The sequence traversal of binary tree is really very important
The implementation idea of sequence traversal of binary tree uses the characteristics of queue, first in first out
First, join the root node
Then queue out, and then queue the left and right subtrees of the out of queue node
The effect of this is that the queue stores the nodes of each layer
2.2 code implementation:
var results = [[Int]]() func levelOrder(_ root: TreeNode?) -> [[Int]] { guard let root = root else{ return [] } var queue = [TreeNode]() queue.append(root) while !queue.isEmpty { var nums = [Int]() for i in 0..<queue.count { let node = queue.removeFirst() nums.append(node.val) if node.left != nil { queue.append(node.left!) } if node.right != nil { queue.append(node.right!) } } results.append(nums) } return results }
3. Depth of binary tree
3.1 core idea:
Use sequence traversal
3.2 code implementation:
func maxDepth(_ root: TreeNode?) -> Int { guard let root = root else { return 0 } var queue = [TreeNode]() queue.append(root) var layer = 0 while !queue.isEmpty { layer += 1 for i in 0..<queue.count { let node = queue.removeFirst() if node.left != nil { queue.append(node.left!) } if node.right != nil { queue.append(node.right!) } } } return layer }
Recursive writing
func maxDepth(_ root: TreeNode?) -> Int { if root == nil { return 0 } return max(maxDepth(root?.left), maxDepth(root?.right)) + 1 }
4. Flip binary tree
4.1 core idea:
since you are flipping the binary tree, you can access each node and exchange the left and right subtrees of each node.
since you want to access no node, you can use either front, middle and back or sequence traversal. The following algorithm is completed by using the preorder traversal.
4.2 code implementation:
func invertTree(_ root: TreeNode?) -> TreeNode? { if root == nil { return root } let temp = root?.left root?.left = root?.right root?.right = temp invertTree(root?.left) invertTree(root?.right) return root }
5. Judge whether a binary tree is a balanced binary tree
5.1 core idea: recursive idea
1. Judge according to the height of the tree
2. If the height difference between the left and right subtrees of the root node is < = 1, the left subtree is balanced, and the right subtree is balanced, return true. Others return false
The ultimate use of recursive ideas.
5.2 code implementation:
func isBalanced(_ root: TreeNode?) -> Bool { if root == nil { return true } return abs(maxDepth(root?.left) - maxDepth(root?.right)) <= 1 && isBalanced(root?.left) && isBalanced(root?.right) } func maxDepth(_ root: TreeNode?) -> Int { if root == nil { return 0 } return max(maxDepth(root?.left), maxDepth(root?.right)) + 1 }
6. Symmetric binary tree
6.1 core idea:
If the left and right subtrees are equal, you need to traverse the left and right subtrees at the same time, and then judge that the values of each node in the corresponding position of the left and right subtrees are equal.
Since we want to traverse every node, and the root node traverses first every time, we can use preorder traversal.
However, because we need to traverse at the same time, we create a method func ismirror (left: treenode?, right: treenode?) - > Bool to traverse the nodes at the corresponding positions on the left and right subtrees at the same time
Core: the setting of this recursive termination condition is very clever
if left == nil && right == nil { return true } if left == nil || right == nil { return false }
6.2 code implementation:
func isSymmetric(_ root: TreeNode?) -> Bool { guard let root = root else { return true } return isMirror(root.left, root.right) } func isMirror(_ left: TreeNode?, _ right: TreeNode?) -> Bool { if left == nil && right == nil { return true } if left == nil || right == nil { return false } return (left!.val == right!.val) && isMirror(left?.left,right?.right) // Because it is symmetrical, it is left vs right, which is different from the judgment of the following question && isMirror(left?.right, right?.left) }
7. Same number
7.1 core idea:
And question 6, is it symmetrical? The idea of solving the tree is essentially the same.
7.2 code implementation:
func isSameTree(_ p: TreeNode?, _ q: TreeNode?) -> Bool { if p == nil && q == nil { return true } if p == nil || q == nil { return false } if p?.val != q?.val { return false } return preorder(p, q) } func preorder(_ p: TreeNode?, _ q: TreeNode?) -> Bool { if p == nil && q == nil { return true } if p == nil || q == nil { return false } if p?.val != q?.val { return false } if preorder(p?.left,q?.left) == false { return false } if preorder(p?.right, q?.right) == false { return false } return true }
Welcome to the "test of quantity" official account, reply [resource]
Python Programming learning resources dry goods
UI automation of Python+Appium framework APP
Python+Selenium framework Web UI automation
Python+Unittest framework API automation
Resources and code are free~
Below the official account is a two-dimensional code, which can be swept directly by WeChat.
Remarks: my personal official account has been officially opened to test technology sharing. It includes: big data test, function test, test development, API interface automation, test operation and maintenance, UI automation test, etc., WeChat search official account: "no way to test" or sweep below the two-dimensional code:
Add attention, let's grow together!