* 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...
* TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ classSolution {//在每一层将节点的左孩子和右孩子分别交换即可,直到节点没有孩子(递归recursion) public: TreeNode* invertTree(TreeNode* root) { TreeNode *temp; if(root) { temp = root->right; root->right = invertTr...
publicTreeNode invertTree(TreeNode root) { if(root ==null)returnnull; Queue<TreeNode> queue =newLinkedList<TreeNode>(); queue.add(root); while(!queue.isEmpty()) { TreeNode current = queue.poll(); TreeNode temp = current.left; current.left = current.right; current.right = temp; if(...
* Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; *///思路:二叉树递归 左右结点交换 在递归翻转左右子树classSolution{public:TreeNode*invertTree(TreeNode*root){T...
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. 翻转二叉树。也是一道不会做,会写homebrew也枉然的题。题干即是题意。例子如下,即层层遍历,把左右子树交换。
226. Invert Binary Tree 提交网址:https://leetcode.com/problems/invert-binary-tree/ Total Accepted:84040Total Submissions:186266Difficulty:EasyACrate:45.1% Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 ...
本篇介绍一下关于二叉树结构很基础的面试题,基础到什么程度呢,引用谷歌的话术: 90% of our engineers use the software you wrote (Homebrew), but you can’t invert a binary tree on a whiteboard so f* off.(连二叉树翻转你都不会,你还玩个毛!)进一步说明了学习数据结构与算法的重要性——为了不被谷...
226. Invert Binary Tree Easy Invert a binary tree. Example: Input: 4 / \ 2 7 / \ / \ 1 3 6 9 1. 2. 3. 4. 5. Output: 4 / \ 7 2 / \ / \ 9 6 3 1 1. 2. 3. 4. 5. Trivia: This problem was inspired bythis original tweetbyMax Howell: ...
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); TreeNode node...
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. 这道题让我们翻转二叉树,是树的基本操作之一,不算难题。最下面那句话实在有些木有节操啊,不知道是Google说给谁的。反正这道题确实难度不大,可以用递归和非递归...