LeetCode 226. Invert Binary Tree (反转二叉树)

Invert a binary tree.

     4
   /     2     7
 / \   / 1   3 6   9

to

     4
   /     7     2
 / \   / 9   6 3   1

Trivia:
This problem was inspired by this original tweet by Max Howell:

Google: 90% of our engineers use the software you wrote (Homebrew), but you can’t invert a binary tree on a whiteboard so fuck off.



题目标签:Tree

  这道题目给了我们一个二叉树,让我们把它反转一下。对于每一个点,它的左边和右边子树需要对调一下。利用postOrder 来遍历树,当走到最低端的时候,点 == null, 返回null, 对于每一个点,把它的left 和right 互换一下。return 这个点。

Java Solution:

Runtime beats 27.97%

完成日期:07/04/2017

关键词:Tree

关键点:把left 和right 互换

 1 /**
 2  * Definition for a binary tree node.
 3  * public class TreeNode {
 4  *     int val;
 5  *     TreeNode left;
 6  *     TreeNode right;
 7  *     TreeNode(int x) { val = x; }
 8  * }
 9  */
10 public class Solution
11 {
12     public TreeNode invertTree(TreeNode root)
13     {
14         if(root == null)
15             return null;
16
17         // continue children
18         invertTree(root.left);
19         invertTree(root.right);
20
21         // swap left and right
22         TreeNode temp;
23         temp = root.left;
24         root.left = root.right;
25         root.right = temp;
26
27         return root;
28     }
29 }

参考资料:N/A

时间: 2024-10-08 09:45:01

LeetCode 226. Invert Binary Tree (反转二叉树)的相关文章

leetcode 226 Invert Binary Tree 翻转二叉树

大牛没有能做出来的题,我们要好好做一做 Invert a binary tree. 4 / 2 7 / \ / 1 3 6 9 to 4 / 7 2 / \ / 9 6 3 1 Trivia: This problem was inspired by this original tweet by Max Howell: Google: 90% of our engineers use the software you wrote (Homebrew), but you can't invert

leetcode 226 Invert Binary Tree

题目连接 https://leetcode.com/problems/invert-binary-tree/ Invert Binary Tree Description Invert a binary tree. 4 / 2 7 / \ / 1 3 6 9 to 4 / 7 2 / \ / 9 6 3 1 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * Tre

Leetcode 226 Invert Binary Tree python

题目: Invert a binary tree. 翻转二叉树. 递归,每次对节点的左右节点调用invertTree函数,直到叶节点. python中也没有swap函数,当然你可以写一个,不过python中可以通过:a, b = b, a交换两个变量的值 1 class Solution(object): 2 def invertTree(self, root): 3 if root == None: return root 4 root.left, root.right = self.inve

Leetcode 226: Invert Binary Tree(二叉树反转)

nvert a binary tree. 4 / 2 7 / \ / 1 3 6 9 to 4 / 7 2 / \ / 9 6 3 1 Trivia: This problem was inspired by this original tweet by Max Howell: Google: 90% of our engineers use the software you wrote (Homebrew), but you can't invert a binary tree on a wh

LeetCode 226 Invert Binary Tree(转换二叉树)

翻译 将下图中上面的二叉树转换为以下的形式.详细为每一个左孩子节点和右孩子节点互换位置. 原文 如上图 分析 每次关于树的题目出错都在于边界条件上--所以这次细致多想了一遍: void swapNode(TreeNode* tree) { if (tree == NULL || (tree->left == NULL && tree->right == NULL)) {} else if (tree->left == NULL && tree->ri

LeetCode Invert Binary Tree 反转二叉树

思路:递归解决,在返回root前保证该点的两个孩子已经互换了.注意可能给一个Null. 1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 */ 10 class Solutio

[LeetCode] 226. Invert Binary Tree JAVA

题目: Invert a binary tree. 4 / 2 7 / \ / 1 3 6 9 to 4 / 7 2 / \ / 9 6 3 1 题意及分析:给出一棵二叉树,对每个节点交换左右子树.对于一个需要交换的节点有三种情况:(1)有左子树,无右子树:(2)有右子树,无左子树:(3)左右子树都有:对着三种情况分别交换左右子树,然后继续对子树递归即可得到结果. 代码: /** * Definition for a binary tree node. * public class TreeNo

Leetcode 226. Invert Binary Tree(easy)

Invert a binary tree. 4 / 2 7 / \ / 1 3 6 9 to 4 / 7 2 / \ / 9 6 3 1 Trivia:This problem was inspired by this original tweet by Max Howell: Google: 90% of our engineers use the software you wrote (Homebrew), but you can't invert a binary tree on a wh

leetcode 226. Invert Binary Tree(递归)

Invert a binary tree. 4 / 2 7 / \ / 1 3 6 9 to 4 / 7 2 / \ / 9 6 3 1 Trivia:This problem was inspired by this original tweet by Max Howell: Google: 90% of our engineers use the software you wrote (Homebrew), but you can’t invert a binary tree on a wh