翻转二叉树

2020-09-16  本文已影响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;
    }
}
上一篇下一篇

猜你喜欢

热点阅读