}TreeNodetmp=root.left; root.left = invertTree(root.right); root.right = invertTree(tmp);returnroot; } } 3.2 Java实现-非递归 publicclassSolution2{publicTreeNodeinvertTree(TreeNode root){if(root ==null) {returnroot;
* 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...
来自专栏 · Leetcode刷题笔记 题目描述 英文版描述 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 = [...
链接:https://leetcode.cn/problems/invert-binary-tree /***Definitionfora binary tree node.*publicclassTreeNode {*intval;*TreeNode left;*TreeNode right;*TreeNode() {}*TreeNode(intval) { this.val=val; }*TreeNode(intval, TreeNode left, TreeNode right) {*this.val=val;*this.left=left;*...
Can you solve this real interview question? Invert Binary Tree - Given the root of a binary tree, invert the tree, and return its root. Example 1: [https://assets.leetcode.com/uploads/2021/03/14/invert1-tree.jpg] Input: root = [4,2,7,1,3,6,9] Outp
首发于LeetCode 切换模式写文章 登录/注册226. 翻转二叉树(Invert Binary Tree) Tye 来自专栏 · LeetCode CategoryDifficulty algorithms Easy (73.33%) Tags tree Companies Unknown Given the root of a binary tree, invert the tree, and return its root. 给出一棵二叉树的根节点root,翻转这棵二叉树,...
【摘要】 这是一道关于二叉树翻转的LeetCode题目,希望对您有所帮助。 目录: 1.Invert Binary Tree - 二叉树翻转 [递归] 题目概述: Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1
Leetcode 257. Binary Tree Paths binarydfsreturnroottree Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 / \ 2 3 \ 5 All root-to-leaf paths are: ["1->2->5", "1->3"] 求二叉树所有从根到叶的路径。 简单DFS /** * Definition...
[LeetCode] 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 bythis original tweetbyMax Howell: Google: 90% of our engineers use the software you wrote (Homebrew), but you ...
Leetcode 226. Invert Binary Tree 0 / 0 / 创建于 5年前 / 利用层序遍历,也就是队列 伪代码: make a queue push root into queue if queue is not empty node= queue pop if has left if left has child enqueue left if has right if right has child enqueue right tmp = node.left node.left...