2.27 Invert Binary Tree(Leetcode 226) post-order解法

 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 class Solution {
11     public TreeNode invertTree(TreeNode root) {
12         // base case
13         if(root == null){
14             return null;
15         }
16         invertTree(root.left);
17         invertTree(root.right);
18         TreeNode tmp = root.left;
19         root.left = root.right;
20         root.right = tmp;
21         return root;
22     }
23 }时间复杂度 O(n)空间复杂度 O(logn)-O(n)

原文地址:https://www.cnblogs.com/juanqiuxiaoxiao/p/8481746.html

时间: 2024-11-05 17:33:50

2.27 Invert Binary Tree(Leetcode 226) post-order解法的相关文章

Invert Binary Tree——LeetCode

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

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

题目连接 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][JavaScript]Invert Binary Tree

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

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

Minimum Depth of Binary Tree leetcode java

题目: Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node. 题解: 递归解法急速判断左右两边子树哪个depth最小,要注意如果有个节点只有一边孩子时,不能返回0,要返回另外一半边的depth. 递归解法: 1     public 

Maximum Depth of Binary Tree leetcode java

题目: Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. 题解: 递归解法: 1     public int maxDepth(TreeNode root) {2         if(root==null)3         

LeetCode226:Invert Binary Tree 二叉树反转

题目 226. Invert Binary Tree QuestionEditorial Solution My Submissions Total Accepted: 109341 Total Submissions: 230799 Difficulty: Easy Invert a binary tree.      4    /     2     7  / \   / 1   3 6   9 to      4    /     7     2  / \   / 9   6 3   1

leetcode_226题——Invert Binary Tree(队列,广度优先搜索)

Invert Binary Tree Total Accepted: 22352 Total Submissions: 62065My Submissions Question Solution 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%