zoukankan      html  css  js  c++  java
  • 101. Symmetric Tree

        /*
         * 101. Symmetric Tree 
         * 2016-5-16 By Mingyang 
         * 刚开始的时候只有一个root做不行,后面改过来,下面是标准的英文回答时间复杂度和空间复杂度的表述
         * Because we traverse the entire input tree once, the total run time is O(n)O(n), 
         * where nn is the total number of nodes in the tree.
         * The number of recursive calls is bound by the height of the tree. 
         * In the worst case, the tree is linear and the height is in O(n)O(n). 
         * Therefore, space complexity due to recursive calls on the stack is O(n)O(n) in the worst case.
         * 当然我们可以用擅长的queue来做
         */
        public boolean isSymmetric(TreeNode root) {
            if (root == null)
                return true;
            return isSymmetricTree(root.left, root.right);
        }
        public boolean isSymmetricTree(TreeNode p, TreeNode q) {
            if (p == null && q == null)
                return true;
            if (p == null || q == null)
                return false;
            return (p.val == q.val) && isSymmetricTree(p.left, q.right)&& isSymmetricTree(p.right, q.left);
        }
  • 相关阅读:
    【Leetcode】113Path Sum II
    【leetcode】112. Path Sum
    virtualbox 中安装win7虚拟机
    制作一个vagrant的win7 box
    socket编程
    异常处理
    strip(),replace()和re.sub()用法
    面象对象 高阶篇
    面象对象 基础篇
    Subprocess模块介绍
  • 原文地址:https://www.cnblogs.com/zmyvszk/p/5500418.html
Copyright © 2011-2022 走看看