- 浏览: 182832 次
- 性别:
- 来自: 济南
文章分类
最新评论
Invert a binary tree.
4
/ \
2 7
/ \ / \
1 3 6 9
to
4
/ \
7 2
/ \ / \
9 6 3 1
给定一个二叉树,交换它的左右子树。我们用递归完成,从根节点开始,递归交换root的左子树,然后递归交换root的右子树。代码如下:
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; } }
发表评论
-
498. Diagonal Traverse
2019-11-15 13:52 264Given a matrix of M x N eleme ... -
496 Next Greater Element I
2019-11-14 13:50 266You are given two arrays (witho ... -
Word Break II
2016-03-09 03:15 381Given a string s and a dictiona ... -
Insert Interval
2016-03-08 02:11 373Given a set of non-overlapping ... -
Merge Intervals
2016-03-07 05:25 497Given a collection of intervals ... -
Merge k Sorted Lists
2016-03-07 04:03 560Merge k sorted linked lists and ... -
Multiply Strings
2016-03-06 07:27 474Given two numbers represented a ... -
N-Queens II
2016-03-06 03:06 661Follow up for N-Queens problem. ... -
N-Queens
2016-03-06 02:47 468The n-queens puzzle is the prob ... -
First Missing Positive
2016-03-05 03:09 427Given an unsorted integer array ... -
Spiral Matrix
2016-03-04 03:39 571Given a matrix of m x n element ... -
Trapping Rain Water
2016-03-04 02:54 576Given n non-negative integers r ... -
Repeated DNA Sequences
2016-03-03 03:10 423All DNA is composed of a series ... -
Increasing Triplet Subsequence
2016-03-02 02:48 894Given an unsorted array return ... -
Maximum Product of Word Lengths
2016-03-02 01:56 928Given a string array words, fin ... -
LRU Cache
2016-02-29 10:37 602Design and implement a data str ... -
Super Ugly Number
2016-02-29 07:07 669Write a program to find the nth ... -
Longest Increasing Path in a Matrix
2016-02-29 05:56 841Given an integer matrix, find t ... -
Coin Change
2016-02-29 04:39 780You are given coins of differen ... -
Minimum Height Trees
2016-02-29 04:11 703For a undirected graph with tre ...
相关推荐
1. Math Implementation Questions(数学实现题) 1.1 Fibonacci Implementation(斐波那契数列实现) ...5.2 Invert Binary Tree(反转二叉树) 5.3... 5.4... 5.5... 6. String Questions(字符串相关问题)
c c语言_leetcode题解之0226_invert_binary_tree
python python_leetcode题解之226_Invert_Binary_Tree.py
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 ...
第 338 章力码 王一飞的LeetCode题解 ...(./solutions/InvertBinaryTree.cc) | 第283话| [MoveZeros.cc] (./solutions/MoveZeros.cc) | 第349话| [IntersectionOfTwoArrays.cc] (./solutions/IntersectionOfTwoArray
14. **Invert Binary Tree**:翻转二叉树。这可以通过递归实现,每个节点的左子树和右子树交换。 15. **Number of 1 Bits**:计算一个整数中1的个数。通过位操作可以快速统计。 16. **Reverse Integer**:反转整数...
LeetCode去除数组重复元素 Arithmetic-Swift ...Invert Binary Tree 两数相加 258. Add Digits 二叉树最深 104. Maximum Depth of Binary Tree 【递归】 遍历求单个数字 136. Single Number 石头游戏 292. Nim Gam
翻转二叉树(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) ...
* [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]...
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 ...
纯JavaScript上的算法来自算法在javascript上完成| 二叉树| | BinaryTreeLevelOrderTraversal.js | | BinaryTreeRightSideView.js | | ConvertSortedArrayToBinarySearchTree.js | | InvertBinaryTree.js | | ...
leetcode卡Leetcode-解决方案 LeetCode DS 日常挑战的解决方案 问题陈述 1.InvertBinaryTree - 2.子序列- 3.SearchInsertPosition - 4.SortColors - 5.单号——
4. 题目226 - "Invert Binary Tree" 题目要求翻转二叉树,即交换每个节点的左右子树。Java实现时,可以使用递归方法,递归地调用翻转函数到每个子节点,直到达到叶子节点,然后逆序返回。 5. 题目230 - "Kth ...
leetcode 树节点leetcode 226 - 反转二叉树 方法一:递归 C# public TreeNode InvertTree ( TreeNode root ) { if ( root == null ) return root ; var temp = root . left ; root . left = root ....
- **二叉树反转**(Invert/Binary Tree Inversion)是指将二叉树中的每个节点的左右子节点进行交换,即原为左子节点的变为右子节点,原为右子节点的变为左子节点。这可以递归地完成,也可以使用两个栈来辅助操作。 ...
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解决方案2020-12-06添加树问题助手Q0226_Invert_Binary_Tree.py中添加了三个助手从列表构建树: buildTree(vals: list)-> TreeNode 按顺序打印遍历树: printTreeInorder(root: TreeNode) 使用BFS...
最后,课件中还提到了一些经典的算法题目,例如“Invert a Binary Tree”(翻转二叉树),这不仅仅是一个编程题目,更是一个考察应聘者对树结构操作理解和编程实现能力的典型例子。面试官通过对这类题目的讲解,向...
<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 ...