My no recursive accepted solution


  • 0
    S

    the idea is just using stack to do preorder Traversal

    public boolean isSymmetric(TreeNode root){
        	if (root == null) return true ;
        	Stack<TreeNode> stack = new Stack <> ();
        	stack.push(root.left) ;
        	stack.push(root.right) ;
        	while (!stack.isEmpty() && root != null) {
        		TreeNode a = stack.pop() ;
        		TreeNode b = stack.pop() ;
        		if (a == null || b == null) {
        			if (a != b) return false ;
        		}
        		if (a != null && a != null) {
        			if (a.val != b.val) return false ;
        			stack.push(a.left);
            		stack.push(b.right);
            		stack.push(a.right);
            		stack.push(b.left);	
        		}    		    		
        	}    	    
        	return true ; 
        }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.