LeetCode101----对称二叉树

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

例如,二叉树 [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

代码如下:
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
public class LeetCode101 {
    public boolean isSymmetric = true;
 
    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
 
        TreeNode(int x) {
            val = x;
        }
    }
 
    public boolean isSymmetric(TreeNode root) {
        if (root == null) {
            return true;
        }
        isSymmetric(root.left, root.right);
        return isSymmetric;
    }
 
    public void isSymmetric(TreeNode left, TreeNode right) {
        if (left == null || right == null) {
            if (left != right) {
                isSymmetric = false;
            }
            return;
        }
        isSymmetric(left.left, right.right);
        isSymmetric(left.right, right.left);
        if (left != null && right != null) {
            if (left.val != right.val) {
                isSymmetric = false;
            }
        }
    }
}

  非递归代码如下所示:

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
public class NonLeetCode101 {
    public boolean isSymmetric = true;
 
    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
 
        TreeNode(int x) {
            val = x;
        }
    }
 
    public boolean isSymmetric(TreeNode root) {
        if (root == null) {
            return true;
        }
        isSymmetric(root.left, root.right);
        return isSymmetric;
    }
 
    public void isSymmetric(TreeNode left, TreeNode right) {
        if (left == null || right == null) {
            if (left != right) {
                isSymmetric = false;
            }
            return;
        }
        if (left.val != right.val) {
            isSymmetric = false;
            return;
        }
        Stack<TreeNode> lStack = lStruct(left);
        Stack<TreeNode> rStack = rStruct(right);
        if (lStack.size() != rStack.size()) {
            isSymmetric = false;
            return;
        }
        System.out.println("栈大小:" + lStack.size());
        while (lStack.size() > 0) {
            TreeNode n1 = lStack.pop();
            TreeNode n2 = rStack.pop();
            if (n1 == null || n2 == null) {
                if (n1 != n2) {
                    isSymmetric = false;
                    return;
                }
            } else if (n1 != null && n2 != null) {
                if (n1.val != n2.val) {
                    isSymmetric = false;
                    return;
                }
            }
        }
    }
 
    private Stack<TreeNode> lStruct(TreeNode root) {
        Stack<TreeNode> stack = new Stack<>();
        Stack<TreeNode> reStack = new Stack<>();
        stack.push(root);
        while (!stack.isEmpty()) {
            root = stack.pop();
            reStack.push(root);
            if (root != null) {
                if (root.right != null) {
                    stack.push(root.right);
                } else if (root.right == null) {
                    stack.push(null);
                }
                if (root.left != null) {
                    stack.push(root.left);
                } else if (root.left == null) {
                    stack.push(null);
                }
            }
        }
        return reStack;
    }
 
    private Stack<TreeNode> rStruct(TreeNode root) {
        Stack<TreeNode> stack = new Stack<>();
        Stack<TreeNode> reStack = new Stack<>();
        stack.push(root);
        while (!stack.isEmpty()) {
            root = stack.pop();
            reStack.push(root);
            if (root != null) {
                if (root.left != null) {
                    stack.push(root.left);
                } else if (root.left == null) {
                    stack.push(null);
                }
                if (root.right != null) {
                    stack.push(root.right);
                } else if (root.right == null) {
                    stack.push(null);
                }
            }
        }
        return reStack;
    }
}

 递归代码如下:

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
public class LeetCode101 {
    public boolean isSymmetric = true;
 
    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
 
        TreeNode(int x) {
            val = x;
        }
    }
 
    public boolean isSymmetric(TreeNode root) {
        if (root == null) {
            return true;
        }
        isSymmetric(root.left, root.right);
        return isSymmetric;
    }
 
    public void isSymmetric(TreeNode left, TreeNode right) {
        if (left == null || right == null) {
            if (left != right) {
                isSymmetric = false;
            }
            return;
        }
        isSymmetric(left.left, right.right);
        isSymmetric(left.right, right.left);
        if (left != null && right != null) {
            if (left.val != right.val) {
                isSymmetric = false;
            }
        }
    }
}

  

posted @   Booker808  阅读(162)  评论(0编辑  收藏  举报
努力加载评论中...
点击右上角即可分享
微信分享提示