Java LeetCode 226. Flip Binary Tree

Flip a binary tree.
Insert picture description here

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    
    
    public TreeNode invertTree(TreeNode root) {
    
    
        reverse(root);
        return root;
    }
    public void reverse(TreeNode root){
    
    
        if(root==null){
    
    
            return;
        }

        TreeNode temp = root.left;
        root.left = root.right;
        root.right = temp;

        reverse(root.left);
        reverse(root.right);
    }
}

Guess you like

Origin blog.csdn.net/sakura_wmh/article/details/110150548