/** 101. Symmetric Tree * 2016-5-16 by Mingyang * Only one root does not work at first, the following is the standard English answer time complexity and spatial complexity of the expression * Because we Trave RSE the entire input tree once, the total run time was 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 are in O (n) o (n). * Therefore, space complexity due to recursive calls on the stack are O (n) o (n) in the worst case. * Of course we can do it with a good queue .*/ Public Booleanissymmetric (TreeNode root) {if(Root = =NULL) return true; returnIssymmetrictree (Root.left, root.right); } Public BooleanIssymmetrictree (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); }
101. Symmetric Tree