代码: /*** Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * }*/publicclassSolution {publicTreeNode invertTree(TreeNode r
Invert a Binary Tree pat-1102 import java.util.Arrays; import java.util.Queue; import java.util.Scanner; import java.util.concurrent.LinkedBlockingQue
* Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */publicclassSolution{publicTreeNodeinvertTree(TreeNode root){if(root==null){// 节点为空,不处理returnnull;}else{// 节点不为空Tree...
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. /** * Definition for a binary tree node. * public class TreeNode { * int val; * ...
Given the root of a binary tree, invert the tree, and return its root. Example 1: Input: root = [4,2,7,1,3,6,9] Output: [4,7,2,9,6,3,1]Example 2: Input: root = [2,1,3] Output: [2,3,1]Example 3:Input: root = [] Output: []Constraints: The number of nodes in ...
Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 1. 2. 3. 4. 5. to 4 / \ 7 2 / \ / \ 9 6 3 1 1. 2. 3. 4. 5. 题解: 既可以自顶向下,也可以自底向上. Time Complexity: O(n). Space: O(logn). AC Java: ...
226. Invert Binary Tree Invert a binary tree. Example: Input: 代码语言:javascript 代码运行次数:0 运行 AI代码解释 4 / \ 2 7 / \ / \ 1 3 6 9 Output: 代码语言:javascript 代码运行次数:0 运行 AI代码解释 4 / \ 7 2 / \ / \ 9 6 3 1 思路: 递归求解翻转每个不为nil的节点 代码:...
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. Java /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) ...
Java: // Runtime: 238 ms/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */publicclassSolution{publicTreeNodeinvertTree(TreeNode root){if(root !=null){TreeNodetemp=root.lef...
(Java) Version 1 Time: 1ms: 简单地递归然后调换左右子树 /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */publicclassSolution{publicTreeNodeinvertTree(TreeNoderoot){if(root==...