美文网首页
翻转二叉树

翻转二叉树

作者: 眼若繁星丶 | 来源:发表于2020-09-16 17:00 被阅读0次

    翻转二叉树


    LeetCode 226

    https://leetcode-cn.com/problems/invert-binary-tree/

    代码如下

    /**
     * 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 = root.left;
            root.left = root.right;
            root.right = temp;
            invertTree(root.left);
            invertTree(root.right);
            return root;
        }
    }

    相关文章

      网友评论

          本文标题:翻转二叉树

          本文链接:https://www.haomeiwen.com/subject/uxrhyktx.html