* 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(r
代码: /*** Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * }*/publicclassSolution {publicTreeNode invertTree(TreeNode root) {if(root==null)returnnull; exchange(root);returnroot; }pub...
TreeNode temp=root.left; root.left=root.right; root.right=temp; invertTree(root.left); invertTree(root.right); returnroot; }
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 ...
JAVA在学者 LeetCode 226 Invert Binary Tree 层序遍历方法 package com.example.demo; import java.util.LinkedList; import java.util.Queue; /** * Created By poplar on 2019/10/30 */ public class InvertBinaryTree { public static void main(String[] args) { TreeNode node = new TreeNode(1); ...
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. 翻转二叉树](https://leetcode-cn.com/problems/invert-binary-tree/) 广度优先遍历深度优先遍历 难度简单743 翻转一棵二叉树。示例:输入: 4 / \ 2 7 / \ / \ 1 3 6 9 输出: 4 / \ 7 2 / \ / \ 9 6 3 1 广度优先遍历 public TreeNode invertTree(T Java架构师必看 2021...
226. Invert Binary Tree leetcode-java文章分类运维 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. Trivia: This problem was inspired by this original tweet by Max Howell:...
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...
leetcodetreenode-leetcode-226-Invert-Binary-Tree:leetcode-226-Invert-二叉 leetcode 树节点leetcode 226 - 反转二叉树 方法一:递归 C# public TreeNode InvertTree ( TreeNode root ) { if ( root == null ) return root ; var temp = root . left ; root . left = root . right ; root . rig...