• leetcode — symmetric-tree


    import java.util.Stack;
    
    /**
     * Source : https://oj.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:
     *
     *     1
     *    / 
     *   2   2
     *  /  / 
     * 3  4 4  3
     *
     * But the following is not:
     *
     *     1
     *    / 
     *   2   2
     *       
     *    3    3
     *
     * Note:
     * Bonus points if you could solve it both recursively and iteratively.
     */
    public class SymmetricTree {
    
        /**
         * 判断一棵树是否是镜像对称的
         *
         * 类比判断两棵树是否相同,将一棵树leftNode,rightNode看做两棵树,判断两棵树是否是镜像对称的
         *
         * 根节点都为空,true
         * 只有一个根节点为空,false
         * 两个根节点都不为空且相同,递归判断两个子节点
         *
         * @param left
         * @param right
         * @return
         */
        public boolean isSymmetric (TreeNode left, TreeNode right) {
            if (left == null && right == null) {
                return true;
            }
            if ((left == null && right != null) || (left != null && right == null)) {
                return false;
            }
            if (left.value != right.value) {
                return false;
            }
            return isSymmetric(left.leftChild, right.rightChild) && isSymmetric(left.rightChild, right.leftChild);
        }
    
    
        /**
         * 使用递归判断是否是镜像对称的
         *
         * 借助栈实现,
         *
         * @param left
         * @param right
         * @return
         */
        public boolean isSymmetricByIterator (TreeNode left, TreeNode right) {
            Stack<TreeNode> leftStack = new Stack<TreeNode>();
            Stack<TreeNode> rightStack = new Stack<TreeNode>();
            leftStack.push(left);
            rightStack.push(right);
            while (leftStack.size() > 0 && rightStack.size() > 0) {
                TreeNode leftNode = leftStack.pop();
                TreeNode rightNode = rightStack.pop();
                if (leftNode == null && rightNode == null) {
                    continue;
                }
                if ((leftNode == null && rightNode != null) || (leftNode != null && rightNode == null)) {
                    return false;
                }
                if (leftNode.value != rightNode.value) {
                    return false;
                }
                leftStack.push(leftNode.leftChild);
                leftStack.push(leftNode.rightChild);
                rightStack.push(rightNode.rightChild);
                rightStack.push(rightNode.leftChild);
            }
            return true;
        }
    
    
        public TreeNode createTree (char[] treeArr) {
            TreeNode[] tree = new TreeNode[treeArr.length];
            for (int i = 0; i < treeArr.length; i++) {
                if (treeArr[i] == '#') {
                    tree[i] = null;
                    continue;
                }
                tree[i] = new TreeNode(treeArr[i]-'0');
            }
            int pos = 0;
            for (int i = 0; i < treeArr.length && pos < treeArr.length-1; i++) {
                if (tree[i] != null) {
                    tree[i].leftChild = tree[++pos];
                    if (pos < treeArr.length-1) {
                        tree[i].rightChild = tree[++pos];
                    }
                }
            }
            return tree[0];
        }
    
    
        private class TreeNode {
            TreeNode leftChild;
            TreeNode rightChild;
            int value;
    
            public TreeNode(int value) {
                this.value = value;
            }
    
            public TreeNode() {
            }
        }
    
    
        public static void main(String[] args) {
            SymmetricTree symmetricTree = new SymmetricTree();
            char[] treeArr1 = new char[]{'1','2','2','3','4','4','3'};
            char[] treeArr2 = new char[]{'1','2','2','#','4','#','4'};
    
            TreeNode tree1 = symmetricTree.createTree(treeArr1);
            TreeNode tree2 = symmetricTree.createTree(treeArr2);
    
    
            System.out.println(symmetricTree.isSymmetric(tree1.leftChild, tree1.rightChild) + "----true");
            System.out.println(symmetricTree.isSymmetricByIterator(tree1.leftChild, tree1.rightChild) + "----true");
    
    
            System.out.println(symmetricTree.isSymmetric(tree2.leftChild, tree2.rightChild) + "----false");
            System.out.println(symmetricTree.isSymmetricByIterator(tree2.leftChild, tree2.rightChild) + "----false");
        }
    
    }
    
    
  • 相关阅读:
    Win7 64位环境下安装mysql5.6
    High CPU Usage 原因及分析
    SQL Server 2012 AlwaysOn 亲身实历记
    elasticsearch的简单介绍
    url().openStream 的超时问题处理
    关于数据库tinyint 字段的值范围
    linux磁盘空间不足预警的相关问题
    关于Dubbo一个接口多个实现的解决方案
    一个优化登录判断的技巧
    SimpleDateFormat的线程安全问题与解决方案
  • 原文地址:https://www.cnblogs.com/sunshine-2015/p/7807071.html
Copyright © 2020-2023  润新知