leetCode---Invert Binary Tree

来源:互联网 发布:netty java实例 编辑:程序博客网 时间:2024/06/13 13:39

一. 题目:Invert Binary Tree

invert a binary tree.

     4   /   \  2     7 / \   / \1   3 6   9
to
     4   /   \  7     2 / \   / \9   6 3   1
Trivia:
This problem was inspired by this original tweet by Max Howell:

二. 思路分析

          题目大意:给定一颗二叉树,反转二叉树的左右节点。思路比较简单:利用递归的思想,代码如下:

public class Solution {    public TreeNode invertTree(TreeNode root) {                if (root == null) {            return null;        }        final TreeNode left = root.left,                right = root.right;        root.left = invertTree(right);        root.right = invertTree(left);        return root;    }}


原创粉丝点击