Invert Binary Tree

Invert Binary Tree

问题:

Invert a binary tree.

     4

   /   \

  2     7

 / \   / \

1   3 6   9

to

     4

   /   \

  7     2

 / \   / \

9   6 3   1

思路:

  简单的递归

我的代码:

public class Solution {

    public TreeNode invertTree(TreeNode root) {

        helper(root);

        return root;

    }

    public void helper(TreeNode root)

    {

        if(root == null)    return;

        TreeNode left = root.left;

        TreeNode right = root.right;

        root.left = right;

        root.right = left;

        helper(left);

        helper(right);

    }

}
View Code

 

你可能感兴趣的:(binary)