Leetcode solution 226. Invert Binary Tree

Problem Statement

Invert a binary tree.

Example:

Input:

     4
   /     2     7
 / \   / 1   3 6   9

Output:

     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 f*** off.

Problem link

Video Tutorial

You can find the detailed video tutorial here

Thought Process

Simple question on understanding about recursion. It could be solved using pre-order and post-order traversal style recursion. It can also be solved iteratively using a queue. Please refer to Leetcode official solution. It‘s very similar to "Binary Tree ZigZag Level Order Traversal". Remember a full binary tree max leaf is (N+1)/2 or ceiling of N/2

, that‘s the O(N) space complexity using a queue.

If you are like Max Howell, you can show off but I would still recommend to stay humble if you really want the job. Brillient jerks are rare (IMHO Linus Torvalds is one) but very productive, but not all companies would accept those culture (Netflix does but not others). Be strong and humble.

Solutions

 1 public TreeNode invertTree(TreeNode root) {
 2     return this.helper(root);
 3 }
 4
 5 // post order
 6 private TreeNode helper(TreeNode root) {
 7     if (root == null) {
 8         return root;
 9     }
10
11     TreeNode l = helper(root.left);
12     TreeNode r = helper(root.right);
13
14     root.left = r;
15     root.right = l;
16
17     return root;
18 }
19
20 // Preorder
21 private TreeNode invertHelper(TreeNode root) {
22     if (root == null) {
23         return null;
24     }
25
26     TreeNode temp = root.left;
27     root.left = root.right;
28     root.right = temp;
29
30     this.invertHelper(root.left);
31     this.invertHelper(root.right);
32
33     return root;
34 } 

Recursion (pre-order and post-order)

Time Complexity: O(N), where N is the total number of tree nodes

Space Complexity: O(1) Or O(lgN) if you count the recursion function stack

References

原文地址:https://www.cnblogs.com/baozitraining/p/12148000.html

时间: 2024-10-11 23:47:50

Leetcode solution 226. Invert Binary Tree的相关文章

Python解Leetcode: 226. Invert Binary Tree

leetcode 226. Invert Binary Tree 倒置二叉树 思路:分别倒置左边和右边的结点,然后把根结点的左右指针分别指向右左倒置后返回的根结点. # Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = x # self.left = None # self.right = None class Solution(object): d

【LeetCode】226. Invert Binary Tree 解题报告

转载请注明出处:http://blog.csdn.net/crazy1235/article/details/51527554 Subject 出处:https://leetcode.com/problems/invert-binary-tree/ Invert a binary tree. 4 / 2 7 / \ / 1 3 6 9 to 4 / 7 2 / \ / 9 6 3 1 Explain 该题目相当简单,就是一个简单的二叉树左右对换结点. Solution solution 1 递归

【一天一道LeetCode】#226. Invert Binary Tree

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源:https://leetcode.com/problems/invert-binary-tree/ Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 (二)解题 题目大意:将一个二叉树倒置.即每个节点的左

LeetCode OJ 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

【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

[LeetCode] NO.226 Invert Binary Tree

[题目] Invert a binary tree. 4 / 2 7 / \ / 1 3 6 9 to 4 / 7 2 / \ / 9 6 3 1 [题目解析] 还是二叉树的问题,这类题目都可以拆解成 处理当前节点,处理左子树,处理右子树 几个子问题.而左子树和右子树的问题也就是当前这类问题,可以使用递归.这种思路是比较简单直接的.参考求二叉树最大深度问题.代码如下. private class TreeNode { int val; TreeNode left; TreeNode right;

226. Invert Binary Tree(C++)

226. Invert Binary Tree Invert a binary tree. 4 / 2 7 / \ / 1 3 6 9 to 4 / 7 2 / \ / 9 6 3 1 代码实现 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), l

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