`

Invert Binary Tree

阅读更多
Invert a binary tree.
       4
     /   \
    2     7
  /   \   /   \
1    3 6    9
to
      4
    /    \
  7      2
/   \     /  \
9   6  3   1

给定一个二叉树,交换它的左右子树。我们用递归完成,从根节点开始,递归交换root的左子树,然后递归交换root的右子树。代码如下:
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public TreeNode invertTree(TreeNode root) {
        if(root == null) return root;
        TreeNode node = root.left;
        root.left = root.right;
        root.right = node;
        invertTree(root.left);
        invertTree(root.right);
        return root;
    }
}
分享到:
评论

相关推荐

    LeetCode和剑指offer中的算法题的题目和解法 和 常见算法汇总

    1. Math Implementation Questions(数学实现题) 1.1 Fibonacci Implementation(斐波那契数列实现) ...5.2 Invert Binary Tree(反转二叉树) 5.3... 5.4... 5.5... 6. String Questions(字符串相关问题)

    c语言-leetcode题解之0226-invert-binary-tree

    c c语言_leetcode题解之0226_invert_binary_tree

    python-leetcode题解之226-Invert-Binary-Tree.py

    python python_leetcode题解之226_Invert_Binary_Tree.py

    Leetcode的ac是什么意思-LeetCodeInJava:leetcode-java

    Invert Binary Tree #237 Delete Node in a Linked List #238 Product of Array Except Self #242 Valid Anagram #258 Add Digits #260 Single Number III #274 H-Index #283 Move Zeroes #292 Nim Game #318 ...

    leetcode338-MyLeetCode:我对LeetCode问题的解答

    第 338 章力码 王一飞的LeetCode题解 ...(./solutions/InvertBinaryTree.cc) | 第283话| [MoveZeros.cc] (./solutions/MoveZeros.cc) | 第349话| [IntersectionOfTwoArrays.cc] (./solutions/IntersectionOfTwoArray

    Leetcode部分试题解析

    14. **Invert Binary Tree**:翻转二叉树。这可以通过递归实现,每个节点的左子树和右子树交换。 15. **Number of 1 Bits**:计算一个整数中1的个数。通过位操作可以快速统计。 16. **Reverse Integer**:反转整数...

    LeetCode去除数组重复元素-Arithmetic-Swift:一些算法的swift实现

    LeetCode去除数组重复元素 Arithmetic-Swift ...Invert Binary Tree 两数相加 258. Add Digits 二叉树最深 104. Maximum Depth of Binary Tree 【递归】 遍历求单个数字 136. Single Number 石头游戏 292. Nim Gam

    lrucacheleetcode-LeetCode:LeetCode刷题

    翻转二叉树(Invert Binary Tree) 2018.9.25 环形链表(Linked List Cycle) 2018.9.25 环形链表 II(Linked List Cycle II) 2018.9.26 删除排序链表中的重复元素 II(Remove Duplicates from Sorted List II) ...

    LeetCode最全代码

    * [Binary Search Tree](https://github.com/kamyu104/LeetCode#binary-search-tree) * [Breadth-First Search](https://github.com/kamyu104/LeetCode#breadth-first-search) * [Depth-First Search]...

    leetcodetreenode-invert-binary-tree:反转二叉树

    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 ...

    JavaScriptAlgorithms:来自leetcode.com的算法在javascript上完成

    纯JavaScript上的算法来自算法在javascript上完成| 二叉树| | BinaryTreeLevelOrderTraversal.js | | BinaryTreeRightSideView.js | | ConvertSortedArrayToBinarySearchTree.js | | InvertBinaryTree.js | | ...

    leetcode卡-Leetcode-solutions:LeetCodeDS日常挑战的解决方案

    leetcode卡Leetcode-解决方案 LeetCode DS 日常挑战的解决方案 问题陈述 1.InvertBinaryTree - 2.子序列- 3.SearchInsertPosition - 4.SortColors - 5.单号——

    Selective_Leetcode_solutions

    4. 题目226 - "Invert Binary Tree" 题目要求翻转二叉树,即交换每个节点的左右子树。Java实现时,可以使用递归方法,递归地调用翻转函数到每个子节点,直到达到叶子节点,然后逆序返回。 5. 题目230 - "Kth ...

    leetcodetreenode-leetcode-226-Invert-Binary-Tree:leetcode-226-Invert-二叉

    leetcode 树节点leetcode 226 - 反转二叉树 方法一:递归 C# public TreeNode InvertTree ( TreeNode root ) { if ( root == null ) return root ; var temp = root . left ; root . left = root ....

    TreeNode.zip

    - **二叉树反转**(Invert/Binary Tree Inversion)是指将二叉树中的每个节点的左右子节点进行交换,即原为左子节点的变为右子节点,原为右子节点的变为左子节点。这可以递归地完成,也可以使用两个栈来辅助操作。 ...

    2sumleetcode-LeetCode:力码

    2sum leetcode 轮廓 1_count_and_say.cpp - super_ugly_number.cpp ...invert_Binary_Tree.cpp - 对称树.cpp - BST_Or_Not.cpp - level_order_traversal.cpp - exponentiation_by_squaring.cpp - Maximum_Depth_B

    LeetCode:LeetCode解决方案

    LeetCode LeetCode解决方案2020-12-06添加树问题助手Q0226_Invert_Binary_Tree.py中添加了三个助手从列表构建树: buildTree(vals: list)-> TreeNode 按顺序打印遍历树: printTreeInorder(root: TreeNode) 使用BFS...

    算法原理与实践课件1_谈面试中的算法和编程准备.pdf

    最后,课件中还提到了一些经典的算法题目,例如“Invert a Binary Tree”(翻转二叉树),这不仅仅是一个编程题目,更是一个考察应聘者对树结构操作理解和编程实现能力的典型例子。面试官通过对这类题目的讲解,向...

    VB编程资源大全(英文源码 控件)

    <END><br>46,FldrView.zip The Folderview ActiveX Control mimics the behaviour of the Windows Explorer Treeview showing the tree structure of the files and folders and other items in the shell's ...

Global site tag (gtag.js) - Google Analytics