zoukankan      html  css  js  c++  java
  • 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");
        }
    
    }
    
    
  • 相关阅读:
    采坑总结01
    Django设置联合唯一约束 -- migrate时报错处理
    Web前端开发资源整理
    kindEditor 使用
    Django模版语言自定义标签-实现前端 关联组合过滤查询
    django views视图函数返回值 return redirect httpresponse总结
    前端图片实现以瀑布流样式显示
    性能优化中CPU、内存、磁盘IO、网络性能的依赖(转)
    几种浏览器内核(百度百科)
    特殊格式文件(视频、声音等) 在数据库中的存储方式
  • 原文地址:https://www.cnblogs.com/sunshine-2015/p/7807071.html
Copyright © 2011-2022 走看看