[Leetcode]226. Invert Binary Tree My Submissions Question

Invert a binary tree.

4
/ \
2 7
/ \ / \
1 3 6 9

to

4
/ \
7 2
/ \ / \
9 6 3 1

很easy的问题,利用递归来进行树结点的转置

public class Solution {
    public TreeNode invertTree(TreeNode root) {
        if(root==null) return root;
        TreeNode tmp = root.left;
        root.left = invertTree(root.right);
        root.right = invertTree(tmp);
        return root;
    }
}

你可能感兴趣的:(LeetCode)