码迷,mamicode.com
首页 > 其他好文 > 详细

力扣(LeetCode)226. 翻转二叉树

时间:2019-04-08 13:54:08      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:思想   inf   rtt   nod   img   solution   src   code   tco   

翻转一棵二叉树。

示例:
技术图片

思想 递归

java版

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public TreeNode invertTree(TreeNode root) {
        if(root == null) {
            return root;
        }
        TreeNode temp = invertTree(root.left);
        root.left = invertTree(root.right);
        root.right = temp;
        return root;
        
    }
}

运行结果

技术图片

力扣(LeetCode)226. 翻转二叉树

标签:思想   inf   rtt   nod   img   solution   src   code   tco   

原文地址:https://www.cnblogs.com/lick468/p/10669954.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!