【07_226】Invert Binary Tree

Invert Binary Tree

Total Accepted: 54994 Total Submissions: 130742 Difficulty: 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 whiteboard so fuck off.

通过这道题,好好理解了递归。

递归返回的是什么要考虑清楚,

可以仔细研磨这道题,很好的题。

C语言

 1 /**
 2  * Definition for a binary tree node.
 3  * struct TreeNode {
 4  *     int val;
 5  *     struct TreeNode *left;
 6  *     struct TreeNode *right;
 7  * };
 8  */
 9 struct TreeNode* invertTree(struct TreeNode* root) {
10     if (root == NULL)
11         return root;
12     if (root->left == NULL && root->right == NULL)
13         return root;
14     else{
15         struct TreeNode* temp = root->right;
16         root->right = root->left;
17         root->left = temp;
18     }
19     root->left = invertTree(root->left);
20     root->right = invertTree(root->right);
21
22     return root;
23 }

下面是在网上找的一种解法,C++写的。由于这是对实际树的结构进行操作,所以说可以不特别管返回值。

1 TreeNode* invertTree(TreeNode* root) {
2     if (root) {
3         invertTree(root->left);
4         invertTree(root->right);
5         std::swap(root->left, root->right);
6     }
7     return root;
8 }
时间: 2024-10-13 12:34:21

【07_226】Invert Binary Tree的相关文章

【leetcode】Construct Binary Tree from Preorder and Inorder Traversal

问题: 给定二叉树的前序和中序遍历,重构这课二叉树. 分析: 前序.中序.后序都是针对于根结点而言,所以又叫作先根.中根.后根(当然不是高跟). 前序:根  左 右 中序:左  根 右 对二叉树,我们将其进行投影,就会发现个有趣的事: 发现投影后的顺序,恰好是中序遍历的顺序,这也就是为什么在构造二叉树的时候,一定需要知道中序遍历,因为中序遍历决定了结点间的相对左右位置关系.所以,对一串有序的数组,我们可以来构建二叉有序数,并通过中序遍历,就可以得到这个有序的数组. 既然中序遍历可以通过根结点将序

【LeetCode】103. Binary Tree Zigzag Level Order Traversal 解题报告

转载请注明出处:http://blog.csdn.net/crazy1235/article/details/51524241 Subject 出处:https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/ Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to ri

【leetcode】145. Binary Tree Postorder Traversal

题目如下: 解题思路:凑数题+3,搞不懂为什么本题的难度是Hard,而[leetcode]590. N-ary Tree Postorder Traversal是Medium. 代码如下: # Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = x # self.left = None # self.right = None class Solutio

【LeetCode】Balanced Binary Tree 解题报告

[题目] Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1. [说明] 不是很难,思路大家可能都会想到用递归,分别判断左右

【LeetCode】145. Binary Tree Postorder Traversal 解题报告

转载请注明出处:http://blog.csdn.net/crazy1235/article/details/51494797 Subject 出处:https://leetcode.com/problems/binary-tree-postorder-traversal/ Hard 级别 Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary tree

【LeetCode】107. Binary Tree Level Order Traversal II 解题报告

转载请注明出处:http://blog.csdn.net/crazy1235/article/details/51508308 Subject 出处:https://leetcode.com/problems/binary-tree-level-order-traversal-ii/ Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to rig

【LeetCode】Construct Binary Tree from Inorder and Postorder Traversal 解题报告

[题目] Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. [解析] 题意:根据二叉树中序遍历和后序遍历的结果,构造该二叉树. 首先明确一下,中序遍历顺序:left - root - right,后序遍历顺序:left  - right - root. 很显然,后序遍历的

【LeetCode】Construct Binary Tree from Preorder and Inorder Traversal 解题报告

[原题] Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. [解析] 题意:根据二叉树先序遍历和中序遍历的结果,构造二叉树.跟 根据中序遍历和后序遍历结果构造二叉树 类似. 先序遍历:root - left - right,中序遍历:left - root - right.

【LeetCode】Flatten Binary Tree to Linked List 解题报告

[题目] Given a binary tree, flatten it to a linked list in-place. For example, Given 1 / 2 5 / \ 3 4 6 The flattened tree should look like: 1 2 3 4 5 6 [解析] 题意是把一棵二叉树按照先序遍历的方式放到一棵只有右支树的二叉树中. 最开始想的思路是递归发,后来发现这样会溢出. 然后就用一个栈和一个队列来实现,队列用来存储先序遍历的结果,栈用于先序遍历.