# 37. 翻转二叉树

java

```java
public class TreeNode {
    public int val;
    public TreeNode left;
    public TreeNode right;

    public TreeNode(int x) {
        val = x;
    }

}


public class TreeUtils {

    public static int k=0;

    public static int k2=0;

    public static TreeNode stringToTree(String s){
        k=0;
        TreeNode root=null;
        root=createTree(root,s);
        return root;
    }

    private static TreeNode createTree(TreeNode node,String s){
        char ch=s.charAt(k++);
        if(ch=='#') return null;
        else{
            node=new TreeNode(ch-'0');
            node.left=createTree(node.left,s);
            node.right=createTree(node.right,s);
            return node;
        }
    }

    public static TreeNode stringToTreeWith(String str,String separator){
        k2=0;
        TreeNode root=null;
        String[] arr=str.trim().split(separator);
        root=createTreeWith(root,arr);
        return root;
    }

    private static TreeNode createTreeWith(TreeNode node,String[] arr){
        String v=arr[k2++];
        if(v.equals("#")) return null;
        else{
            node=new TreeNode(Integer.valueOf(v));
            node.left=createTreeWith(node.left,arr);
            node.right=createTreeWith(node.right,arr);
            return node;
        }
    }

    /**
     * 先序遍历
     * @param root
     */
    public static void travser(TreeNode root){
        if(root!=null){
            System.out.print(root.val+" ");
            travser(root.left);
            travser(root.right);
        }
    }

    /**
     * 中序遍历
     * @param root
     */
    public static void travser2(TreeNode root){
        if(root!=null){
            travser2(root.left);
            System.out.print(root.val+" ");
            travser2(root.right);
        }
    }

    /**
     * 后序遍历
     * @param root
     */
    public static void travser3(TreeNode root){
        if(root!=null){
            travser3(root.left);
            travser3(root.right);
            System.out.print(root.val+" ");
        }
    }
}



/**
 * 226. 翻转二叉树
 *
 * 翻转一棵二叉树。
 *
 * 示例：
 *
 * 输入：
 *
 *      4
 *    /   \
 *   2     7
 *  / \   / \
 * 1   3 6   9
 * 输出：
 *
 *      4
 *    /   \
 *   7     2
 *  / \   / \
 * 9   6 3   1
 * 备注:
 * 这个问题是受到 Max Howell 的 原问题 启发的 ：
 *
 * 谷歌：我们90％的工程师使用您编写的软件(Homebrew)，但是您却无法在面试时在白板上写出翻转二叉树这道题，这太糟糕了。
 */
public class Solution226 {
    public TreeNode invertTree(TreeNode root) {
        doThing(root);
        return root;
    }

    public void doThing(TreeNode root) {
        if(root!=null){
            doThing(root.left);
            doThing(root.right);
            TreeNode tmp=root.left;
            root.left=root.right;
            root.right=tmp;
        }
    }

    public static void main(String[] args){
        TreeNode root=TreeUtils.stringToTree("421##3##76##9##");
        TreeNode r=new Solution226().invertTree(root);
        TreeUtils.travser(r);
    }
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://im-qianuxn.gitbook.io/pytorch/ji-suan-ji/shua-ti/shu-ju-jie-gou-lei/37-fan-zhuang-er-cha-shu.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
