Skip to content

Latest commit

 

History

History
42 lines (34 loc) · 554 Bytes

File metadata and controls

42 lines (34 loc) · 554 Bytes

226. Invert Binary Tree

Problem

nvert a binary tree.

 4

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

tag:

Solution

如果节点为空,返回 否则

  • 递归转换左子树
  • 递归转换右子树
  • 对换左右子树

java

    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;
    }

go