package com.leetcode.leetcode.tree;

/**
 * 翻转一棵二叉树。
 */
public class InvertTree {
  public class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;
    TreeNode(int x) { val = x; }
  }
  public TreeNode invertTree(TreeNode root) {
    if (root == null) {
      return null;
    }
    TreeNode right = invertTree(root.right);
    TreeNode left = invertTree(root.left);
    root.left = right;
    root.right = left;
    return root;
  }

}

github同步

You Might Also Like

No Comments

Leave a Reply