How to understand python symmetric binary tree
Python symmetric binary tree how to understand, I believe that many inexperienced people do not know what to do, so this paper summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.
Algorithm:
A symmetric binary tree is a concept of mirror image:
For example, a symmetric binary tree draws a line vertically along the root node, and then the left and right subtrees on both sides fold together to overlap. This is the symmetric binary tree. The specific scene is as follows:
1. An empty node and a node are symmetrical 2. Only the nodes of the left subtree or the right subtree are not symmetric nodes 3. The parent node is the same, the left node of the left subtree = = the right node of the right subtree, and the right node of the left subtree = = the left node of the right subtree, which is also a symmetric node. (note: recursive implementation of these judgments is fine.)
Topic 1: symmetric binary tree
Code implementation:
/ * Definition for a binary tree node. * type TreeNode struct {* Val int * Left * TreeNode * Right * TreeNode * * / func isSymmetric (root * TreeNode) bool {if root = = nil {return true} return check (root.Left,root.Right)} func check (l R * TreeNode) bool {if l==nil & & r = = nil {return true} if l==nil | | r = = nil {return false} return l.Val = = r.Val & & check (l.LeftMagazine r.right) & & check (l.RightZhir.left)} / algorithm: the concept of mirroring The left subtree of the L tree is the same as the right subtree of the R tree, and the right subtree of the L tree is the same as the left subtree of the R tree. After reading the above, have you mastered how to understand the python symmetric binary tree? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!