文章

LeetCode Symmetric Tree

Problem

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

For example, this binary tree [1,2,2,3,4,4,3] is symmetric:

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

But the following [1,2,2,null,3,null,3] is not:

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

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

判断一个tree是不是自对称的。建议采用递推和递归两种解法。

1
2
3
4
5
6
7
8
9
public class TreeNode {
        int val;
        SymmetricTree.TreeNode left;
        SymmetricTree.TreeNode right;

        TreeNode(int x) {
            val = x;
        }
    }

Java 实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package com.coderli.leetcode.algorithms.easy;

import java.util.Stack;

/**
 * Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
 * <p>
 * For example, this binary tree [1,2,2,3,4,4,3] is symmetric:
 * <p>
 * 1
 * / \
 * 2   2
 * / \ / \
 * 3  4 4  3
 * But the following [1,2,2,null,3,null,3] is not:
 * 1
 * / \
 * 2   2
 * \   \
 * 3    3
 * Note:
 * Bonus points if you could solve it both recursively and iteratively.
 *
 * @author OneCoder 2017-11-11 21:27
 */
public class SymmetricTree {

    public static void main(String[] args) {
        SymmetricTree symmetricTree = new SymmetricTree();
        SymmetricTree.TreeNode tree = symmetricTree.new TreeNode(1);
        SymmetricTree.TreeNode subNodeLeft = symmetricTree.new TreeNode(2);
        SymmetricTree.TreeNode subNodeRight = symmetricTree.new TreeNode(2);
        subNodeLeft.left = symmetricTree.new TreeNode(3);
        subNodeLeft.right = symmetricTree.new TreeNode(4);
        subNodeRight.left = symmetricTree.new TreeNode(4);
        subNodeRight.right = symmetricTree.new TreeNode(3);
        tree.left = subNodeLeft;
        tree.right = subNodeRight;
        System.out.println(symmetricTree.isSymmetric(tree));
    }


    public boolean isSymmetric(TreeNode root) {
        if (root == null) {
            return true;
        }
        Stack<TreeNode> leftStack = new Stack<>();
        Stack<TreeNode> rightStack = new Stack<>();
        leftStack.push(root.left);
        rightStack.push(root.right);
        while (!leftStack.isEmpty() && !rightStack.isEmpty()) {
            TreeNode leftSide = leftStack.pop();
            TreeNode rightSide = rightStack.pop();
            if (leftSide == null && rightSide == null) {
                continue;
            }
            if (leftSide == null || rightSide == null) {
                return false;
            }
            if (leftSide.val != rightSide.val) {
                return false;
            }
            leftStack.push(leftSide.right);
            leftStack.push(leftSide.left);
            rightStack.push(rightSide.left);
            rightStack.push(rightSide.right);
        }
        return leftStack.isEmpty() && rightStack.isEmpty();
    }

    // recursively
    public boolean isSymmetricRecursively(TreeNode root) {
        if (root == null) {
            return true;
        }
        return compare(root.left, root.right);
    }

    private boolean compare(TreeNode leftSide, TreeNode rightSide) {
        if (leftSide == null && rightSide == null) {
            return true;
        }
        if (leftSide == null || rightSide == null) {
            return false;
        }
        if (leftSide.val != rightSide.val) {
            return false;
        }
        return compare(leftSide.left, rightSide.right) && compare(leftSide.right, rightSide.left);
    }

    public class TreeNode {
        int val;
        SymmetricTree.TreeNode left;
        SymmetricTree.TreeNode right;

        TreeNode(int x) {
            val = x;
        }
    }

}


分析

递归似乎没什么好解释的。即左左=右右,左右=右左。

递推,就跟树的遍历一样,借助一个栈即可。

本文由作者按照 CC BY 4.0 进行授权