首页 > 其他 > 详细

翻转二叉树

时间:2021-07-15 23:56:20      阅读:40      评论:0      收藏:0      [点我收藏+]

226. Invert Binary Tree

Given the root of a binary tree, invert the tree, and return its root.

Example 1

技术分享图片

Input: root = [4,2,7,1,3,6,9]
Output: [4,7,2,9,6,3,1]

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

        TreeNode tmp=root.left;
        root.left=root.right;
        root.right=tmp;

        invertTree(root.left);
        invertTree(root.right);

        return root;

    }
}

翻转二叉树

原文:https://www.cnblogs.com/dss-99/p/15017949.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!