Java: Clean Code: Very easy to understand; Classic Post Order Traversal


  • 0
    S
    public class Solution {
        public TreeNode invertTree(TreeNode root) {
            reverse(root);
            return root;
        }
        public void reverse(TreeNode root){
            if (root != null){
                reverse(root.left);    //traverse left
                reverse(root.right);   //traverse right
                //Swapping at root
                TreeNode temp = root.left;
                root.left = root.right;
                root.right = temp;            
            }
        }
    }
    

Log in to reply
 

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