LeetCode-101

Links:https://leetcode.com/problems/symmetric-tree/

Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).

For example, this binary tree is symmetric:

But the following is not:

Note:
Bonus points if you could solve it both recursively and iteratively.

题目大意:判断是否对称,要求用循环 和 递归 两种方法做。

 

递归思路:比较左右节点,然后 递归比较 左左和右右,左右和右左(对称)

 

循环思路:用两个栈存储,反向进行push。对于

这样的,两个栈的插入顺序如下:

L:2,3,4.分别是(中,左,右)

R:2,3,4.分别是(中,右,左)

至于比较顺序,不需要管它,因为可以保证是相同的。

【LeetCode】101. Symmetric Tree
Tagged on:
0 0 投票数
Article Rating
订阅评论
提醒

0 评论
内联反馈
查看所有评论