My simple Java solution accepeted, using a Queue


  • 4
    A
        public TreeNode invertTree(TreeNode root) {
        Queue<TreeNode> processedNodes = new LinkedList<TreeNode>();
        
        if(root!=null){
            processedNodes.add(root);
            while(!processedNodes.isEmpty()){
                //remove node from queue, process current node
                TreeNode current = processedNodes.remove();
                
                //swap nodes
                TreeNode tmp = current.left;
                current.left = current.right;
                current.right = tmp;
                
                if(current.left!=null){
                    processedNodes.add(current.left);
                }
                
                if(current.right!=null){
                    processedNodes.add(current.right);
                }
                
            }
        }
        return root;
    }

Log in to reply
 

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