检查二叉树是镜像还是对称
测试树是否对称的基本算法是什么?因为它是一棵二叉树,所以我假设它是排序的递归定义
正式问题如下:
如果二叉树的左子树和右子树是相同的镜像,则二叉树是自身的镜像,即二叉树是对称的。 通过几个例子可以最好地解释这一点。
1
/ \
2 2
TRUE
1
/ \
2 2
\
3
FALSE
1
/ \
2 2
/ \ / \
4 3 3 4
TRUE
1
/ \
2 2
/ \ / \
3 4 3 4
FALSE
1
/ \
2 2
/ \
3 3
TRUE
在选择的编程语言中,定义 BTree 类/C 结构和关联的方法来检查树是否是镜像。对于静态类型语言,您可以假设节点值都是整数。
Class/structure definition
BTree {
BTree left;
BTree right;
int value;
}
假设调用者跟踪树的根并在其上调用函数 isMirror()。
另外,如果定义类,如果数据元素不可公开访问,请确保提供无参构造函数和 getter/setter 方法。
What is the basic algorithm for testing if a tree is symmetrical? Because it is a binary tree, I would assume that it would be a recursive definition of sorts
The formal question is below:
A binary tree is a mirror image of itself if its left and right subtrees are identical mirror images i.e., the binary tree is symmetrical.
This is best explained with a few examples.
1
/ \
2 2
TRUE
1
/ \
2 2
\
3
FALSE
1
/ \
2 2
/ \ / \
4 3 3 4
TRUE
1
/ \
2 2
/ \ / \
3 4 3 4
FALSE
1
/ \
2 2
/ \
3 3
TRUE
In a programming language of choice, define a BTree class/C struct and an associated method to check if the tree is a mirror image. For statically typed languages, you can assume that node values are all integers.
Class/structure definition
BTree {
BTree left;
BTree right;
int value;
}
Assume that the root of the tree is tracked by caller and function isMirror() is invoked on it.
Also, if defining a class, make sure to provide a no-argument constructor and getter/setter methods if data elements are not publicly accessible.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(17)
java脚本解决算法怎么样
使用
araay = ["10" , "2", "2", "#", "1", "1", "#"]
让我们将其分解为每个步骤,
检查除第一步之外的每个步骤的镜像,
让我们执行步骤 3 。
第 3 步 = ["#", "1", "1", "#"]
从中间将其分成 2 个数组
现在反转步骤 3 2
现在检查步骤 3 2 是否等于步骤 3 1
对每个步骤执行上述操作,
如果所有步骤都是镜像,则二叉树是镜像
how about a java script solution
algorithm used
araay = ["10" , "2", "2", "#", "1", "1", "#"]
lets break this into each step
check mirror for each step other than first step
lets take step 3 .
step 3 = ["#", "1", "1", "#"]
break this into 2 array from middle
Now reverse step 3 2
now check if step 3 2 is equal to step 3 1
Do the above thing for each step
if all are mirror then binary tree is mirror
我经验不是很丰富(在公司工作了一年),但在我看来,这可以通过使用 S=recursion 来解决
例如:
如果匹配则返回 true。
I'm not very experienced (just a year at corporate), but in my opinion, this can be solved by using S=recursion
For example:
Return true if it matches.
如何在以下函数上调用mirrorEquals(root.left, root.right): -
基本上比较左子树和反转右子树,在根上绘制一条假想的反转线。
How about calling mirrorEquals(root.left, root.right) on the following function :-
Basically compare the left subtree and inverted right subtree, drawing an imaginary line of inversion across root.
解决方案 1 - 递归:
解决方案 2 - 迭代:
Solution 1 - Recursively:
Solution 2 - Iteratively:
使用上述方法的Java 中的递归和迭代解决方案
递归
迭代使用LinkedList作为队列 em>
测试用例
树节点类
Recursive and Iterative solutions in Java using approaches discussed above
Recursive
Iterative using LinkedList as a Queue
Test Case
Tree Node class
@gvijay 的递归解决方案非常清晰,这是一个迭代解决方案。
从上到下检查树的每一行,看看这些值是否是回文。如果它们都是,那么,是的,它是一面镜子。您需要实现一种算法来访问每一行并包含稀疏树的空值。用伪代码表示:
技巧是设计迭代树的行的算法,并考虑稀疏树应将空值作为占位符。这个 Java 实现看起来没问题:
The recursive solution from @gvijay is very clear, and here's an iterative solution.
Inspect each row of the tree from top to bottom and see if the values are a palindrome. If they all are then, yes, it's a mirror. You'll need to implement an algorithm to visit each row and include null values for sparse trees. In pseudocode:
The trick is to design the algorithm to iterate the rows of a tree with consideration that sparse trees should have null values as place holders. This Java implementation seems ok:
编辑
正如评论中所指出的,我的算法的第一个版本对于某些输入失败了。我不会重新发明轮子,我只会使用 @gvijay 正确的算法提供一个 Python 答案。首先,二叉树的表示:
我使用问题中的所有示例树和返回错误结果的树测试了上述代码,如评论中所述。现在所有情况的结果都是正确的:
EDIT
As was pointed out in the comments, my first version of the algorithm failed for certain inputs. I'm not going to reinvent the wheel, I'll just provide a Python answer using @gvijay correct algorithm. First, a representation for the binary tree:
I tested the above code using all the sample trees in the question and the trees which were returning incorrect results, as mentioned in the comments. Now the results are correct for all cases:
这是 gvijay 的 C++ 解决方案
Here is a C++ solution per gvijay
下面是关于 C-COde 的解决方案
Below is the solution with respect to C- COde
如果有人需要 Swift 版本,这里有一个。
另一种方法是仅反转一个子树,并以直接的方式比较两个结果子树。
If someone needs a Swift version, here's one.
Another approach would be to just invert one of the subtrees, and compare the two resulting subtrees in a straightforward manner.
方法略有不同。
如何对二叉树进行中序遍历,将所有内容存储在某些数据结构(如字符串/数组)中。
遍历完成后,检查数组中的元素是否形成回文。
空间效率不高(递归需要 O(log(n)),此方法的时间复杂度为 O(n)),但这也可以。
Slightly different approach.
How about do an inorder traversal of the binary tree storing all the contents in some data structure like a string/ array.
Once traversal is complete, check if the elements in your array form a palindrome.
Not as efficient space wise (recursion takes O(log(n)), this method tales O(n)) but this will work as well.
在 python 中使用略有不同的方法的迭代解决方案。使用queue1按从左到右的顺序存储左子节点,使用queue2按从右到左的顺序存储右子节点并比较是否相等。
Iterative solution using slightly different approach in python. Use queue1 to store left children in order of left to right and queue2 to store right children in order of right to left and compare for equality.
使用Python
using python
我想我会在 Python 中添加一个解决方案,有些人可能会发现它比其他方法更容易理解。这个想法是:
+1
添加到左孩子返回的值中。-1
添加到右子节点返回的值中。l+r
返回给父级因此,如果树中的任何节点
l+r == 0
,则锚定在该节点的子树是对称的。因此,仅当根处l+r == 0
时,整个树才是对称的。Thought I'd add a solution in Python that some people might find easier to understand than other approaches. The idea is:
+1
to value returned by left child.-1
to value returned by right child.l+r
to parentSo if
l+r == 0
for any node in the tree, then the sub-tree anchored at that node is symmetric. Therefore the entire tree is symmetric only ifl+r == 0
at root.使用队列,因为我发现递归有点难。
Using Queue , Because i find Recursion a bit hard.