19. 树 - 树的对称

1.题目

LeetCode: 101. 对称二叉树

【easy】

给定一个二叉树,检查它是否是镜像对称的。

例如,二叉树 [1,2,2,3,4,4,3] 是对称的。

    1
   / \
  2   2
 / \ / \
3  4 4  3

但是下面这个 [1,2,2,null,3,null,3] 则不是镜像对称的:

    1
   / \
  2   2
   \   \
   3    3

进阶:

你可以运用递归和迭代两种方法解决这个问题吗?

2.解题

方法一:递归法

当树为空时,返回true

否则递归判断左子树和右子树是否对称,即判断左子树的左孩子和右子树的右孩子是否一样。

java:

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public boolean isSymmetric(TreeNode root) {
        if (root == null) return true;
        return isSymmetric2(root.left, root.right);
    }

    public boolean isSymmetric2(TreeNode t1, TreeNode t2) {
        if (t1 == null && t2 == null) return true;
        if (t1 == null || t2 == null) return false;
        if (t1.val != t2.val) return false;
        return isSymmetric2(t1.left, t2.right) && isSymmetric2(t1.right, t2.left);
    }
}

时间复杂度:O(n),由于要遍历树的每一个节点

空间复杂度:O(n)

方法二:迭代法

采用队列进行存储,判断左子树和右子树是否为对称。

每次加入队列的顺序是:左子树的左孩子,右子树的右孩子,左子树的右孩子,右子树的左孩子。

java:

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public boolean isSymmetric(TreeNode root) {
        if (root == null) return true;
        return isSymmetric2(root.left, root.right);
    }

    public boolean isSymmetric2(TreeNode t1, TreeNode t2) {
        Queue<TreeNode> queue = new LinkedList<TreeNode>();
        queue.offer(t1);
        queue.offer(t2);
        while (!queue.isEmpty()) {
            TreeNode s = queue.poll();
            TreeNode t = queue.poll();
            if (s == null && t == null) continue;
            if (s == null || t == null) return false;
            if (s.val != t.val) return false;
            queue.offer(s.left);
            queue.offer(t.right);

            queue.offer(s.right);
            queue.offer(t.left);
        }
        return true;
    }
}

时间复杂度:O(n),由于要遍历树的每一个节点

空间复杂度:O(n)

我将为您介绍如何使用C# GDI+绘制美观的图案,特别是基于黄金比例的黄金矩形图案,这是一种在艺术和设计中广泛应用的美学比例。 ### 黄金矩形绘制原理 黄金矩形是一种长宽比满足黄金比例(约1.618)的矩形,数学表达式为: $$ \frac{a+b}{a} = \frac{a}{b} = \phi \approx 1.6180339887 $$ 其中$\phi$是黄金比例常数。 ### C# GDI+实现步骤 ```csharp // 在Panel的Paint事件中绘制 private void panel1_Paint(object sender, PaintEventArgs e) { Graphics g = e.Graphics; g.SmoothingMode = SmoothingMode.AntiAlias; // 开启抗锯齿 // 初始矩形尺寸(基于黄金比例) float phi = (1 + (float)Math.Sqrt(5)) / 2; // 黄金比例φ float startWidth = panel1.Width * 0.8f; float startHeight = startWidth / phi; PointF center = new PointF(panel1.Width / 2, panel1.Height / 2); int layers = 12; // 嵌套层数 float rotationAngle = 5f; // 旋转增量 // 渐变色画笔 Color startColor = Color.Gold; Color endColor = Color.DarkGoldenrod; for (int i = 0; i < layers; i++) { // 计算当前矩形尺寸 float width = startWidth * (float)Math.Pow(1/phi, i); float height = width / phi; // 创建渐变画笔 using (Pen pen = new Pen(InterpolateColor(startColor, endColor, i/(float)layers), 2)) { // 设置坐标变换 g.ResetTransform(); g.TranslateTransform(center.X, center.Y); g.RotateTransform(i * rotationAngle); // 绘制矩形(以中心为基准) g.DrawRectangle(pen, -width/2, -height/2, width, height); // 可选:绘制内切圆 if (i % 3 == 0) { using (Brush brush = new SolidBrush(Color.FromArgb(100, 255, 215, 0))) { g.FillEllipse(brush, -width/2, -height/2, width, height); } } } } // 添加中心装饰 using (Brush centerBrush = new SolidBrush(Color.DarkOrange)) { g.ResetTransform(); g.FillEllipse(centerBrush, center.X - 10, center.Y - 10, 20, 20); } } // 颜色插值方法 private Color InterpolateColor(Color start, Color end, float ratio) { int r = (int)(start.R + (end.R - start.R) * ratio); int g = (int)(start.G + (end.G - start.G) * ratio); int b = (int)(start.B + (end.B - start.B) * ratio); return Color.FromArgb(255, r, g, b); } ``` ### 关键技巧说明 1. **黄金比例计算**: ```csharp float phi = (1 + (float)Math.Sqrt(5)) / 2; // ≈1.618 float height = width / phi; // 保持黄金比例 ``` 2. **坐标变换技术**: ```csharp g.TranslateTransform(center.X, center.Y); // 平移到中心 g.RotateTransform(angle); // 旋转坐标系 ``` 3. **抗锯齿处理**: ```csharp g.SmoothingMode = SmoothingMode.AntiAlias; // 使边缘平滑 ``` 4. **渐变色生成**: ```csharp Color newColor = Color.FromArgb( start.R + (int)((end.R - start.R) * ratio), ... // RGB分量插值 ); ``` ### 效果优化建议 1. **动态效果**:添加定时器实现旋转动画 ```csharp Timer timer = new Timer { Interval = 50 }; timer.Tick += (s, e) => { rotationAngle += 1; panel1.Invalidate(); }; timer.Start(); ``` 2. **交互功能**:响应鼠标移动改变图案 ```csharp panel1.MouseMove += (s, e) => { center = e.Location; panel1.Invalidate(); }; ``` 3. **纹理填充**:使用TextureBrush添加复杂纹理 ```csharp using (TextureBrush brush = new TextureBrush(Properties.Resources.MarbleTexture)) { g.FillRectangle(brush, rect); } ``` ### 其他美观图案方案 1. **分形**:递归绘制形结构 2. **曼德勃罗集**:实现数学分形图案 3. **万花筒效果**:基于对称反射的图案 4. **粒子系统**:模拟自然现象(烟花/水流) > 提示:使用`GraphicsPath`组合复杂形状,通过`PathGradientBrush`实现高级渐变效果,能让图案更加精美。
最新发布
08-12
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值