Invert Binary Tree

题目:

Invert a binary tree.

分析:

3min做完,没有bug,还是要考虑一下不能pass reference这个情况

解法:

public class Solution {
    /**
     * @param root: a TreeNode, the root of the binary tree
     * @return: nothing
     */
    public void invertBinaryTree(TreeNode root) {
        // write your code here
        if (root == null) {
            return;
        }
        TreeNode temp = root.left;
        root.left = root.right;
        root.right = temp;
        if (root.left != null) {
            invertBinaryTree(root.left);
        }
        if (root.right != null) {
            invertBinaryTree(root.right);
        }
    }
}

results matching ""

    No results matching ""