Simple Recursive Java Solution


  • 0
    D
    public class Solution {
        public void swapNode(TreeNode node){
            TreeNode tmp = node.left;
            node.left = node.right;
            node.right = tmp;
        }
        public TreeNode invertTree(TreeNode root) {
            if(root == null) return null;
            if(root.left != null) invertTree(root.left);
            if(root.right != null) invertTree(root.right);
            if(root.right != null || root.left != null) swapNode(root);
            return root;
        }
    }

Log in to reply
 

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